i'm building my first Django app so i'm know i'm missing a lot of things, but i installed a gallery for my site and is inside site-packages, i set the url inside urls.py and it looks like this :
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I can perfectly access the gallery and its functionalities if i write the url in the browser, but i cannot reference the gallery Url inside my template like the other Urls using {% url 'gallery' %}, i keep getting this error :
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
Also heres the gallery url:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
Thanks!
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
Change it as shown above and call gallery:album_list for album list, and follow same pattern for others
Related
I'm not able to access the url named "forum" using the reverse function. It gives me this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'forum' not found. 'forum' is not a valid view function or pattern name.
I'm able to access all my other namespaces URLs within the other apps. But I just can't access anything by name from my root URLs.py file.
Here is my root URLs.py file
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls), name='forum'),
path('admin/', admin.site.urls),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Remove:
from machina import urls as machina_urls
Change the url to this:
path('forum/', include(machina.urls),),
in the urls.py file of machina add above the urlpatterns:
app_name = 'machina'
If you are going to use reverse remember:
reverse('app_name:url_name')
For example we have the following url in our machina.urls:
path('bitcoin/', views.bitcoin, name='bitcoin'),
Your reverse would look like this:
reverse('machina:bitcoin')
docs: https://docs.djangoproject.com/en/3.1/ref/urls/#include << Class based overview
Examples: https://docs.djangoproject.com/en/3.1/topics/http/urls/#namespaces-and-include << Copy and write overview
Other option is to have a separate file with url pattern lists for example:
url_list.py
from django.urls import path
from yesyes import views
varvar = [
path('', views.testbased, name='index'),
]
urls.py:
from django.urls import path, include
from yesyes.url_list import varvar as urllist
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',include(urllist), name='test'),
]
My root urls.py
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')), # new
]+static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
My pages app urls.py
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('', views.home_page, name='home_page'),
path('<tab>', views.home,name='home'),
]
With this, I am able to access
127.0.0.1:8000/Home
127.0.0.1:8000/About
127.0.0.1:8000/Services
127.0.0.1:8000/Portfolio
All the tabs with url entry.
But, when I create an url entry in html template, {% url 'About' %}
getting NoReverseMatch
Very clever - I see you have 'short circuited' adding extra url patterns with your 'tab' path.
The issue is that django - specifically your templates - do not know what url 'About' refers to as you have not declared it as a name to any route in your url patterns.
i.e. something like:
path('/path/to/about', views.about_function, name='About')
What you can do in your template is hard code the path, for example:
<a href='/hardcode/this/path'> About </a>
Just note that if you change the 'About' page path, you'll need to replace it everywhere it's defined in any of your templates - plus possibly some other side effects.
Right now what I am doing is this:
pages app > urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
project > urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
This seems to work fine. I am able to go to the homepage and the about page fine, but I have seen other people do as in the following:
project > urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
path('about/', include('pages.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
So basically, in the project urls file, the line
'path('about/', include('pages.urls')),'
is added as well as keeping the pages url the same as above.
So I was wondering what is the correct way of linking the about page in the project urls file.
If you have more than a single view and a single url for it in your app and you want to put all of them after some url prefix you put that prefix in urls.py root.
So lets say you have products and categories views and you want them behind api prefix. That means /api/products/ and /api/categories/ then you would have in your "api" app:
from django.urls import path
from . import views
urlpatterns = [
path('products', views.products, name='products'),
path('categories', views.categories, name='categories'),
]
In your root urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
in pages app you have to create urls.py file and link the url to the view
urlpatterns = [
path('about/', views.about, name='about'),]
and in templates you can call the url with its name
register.html is not accessible on 'register/' url
tried changing url to '' and webpage is accessible but not on 'register/'
projects urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('login/',include('login.urls')),
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
#path('login/',),
path('register/',views.registerView, name="register_url"),
#path('logout/',),
]
views.py
from django.shortcuts import render
def indexView(request):
return render(request,'index.html')
def dashboardView(request):
return render(request,'dashboard.html')
def registerView(request):
return render(request,'register.html')
templates/register.html
Either you type login/ before evry URL pattern of apps urls.py file while testing URL.
or
You should include url mapping of apps urls.py in main urls.py
This might help you.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('apps.urls')), #put you app name here
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
path('register/',views.registerView, name="register_url"),
]
This will work and you can add login/ url mapping inside the apps urls.py.
This type of flow will make it easy to understand .
Use url_name for URL calling:
{% url 'register_url' %}
This will directly search for 'register_url' in the project and will return the path.
url_name must be unique so make sure your url_name is uniquely defined.
I have the following http address when running in development (manage.py runserver)
http://127.0.0.1:8000/
When using wamp i use an alias named picon
http://localhost/picon/
When clicking on links from the nav bar the navigation works fine for both deployments. But when i click from the following views.py the urls break;
def addcustomer(request):
form = AddCustomerForm(request.POST or None)
if form.is_valid():
save_it = form.save(commit=False)
save_it.save()
messages.success(request, 'Customer added succesfully')
return HttpResponseRedirect('/picon/customers')
return render_to_response("addcustomer.html",
locals(),
context_instance=RequestContext(request))
In the production server i use the prefix /picon/ because otherwise the link can not be found when using an alias. Off course this alias is not available on the development version.
So my question, can i create a dynamic alias that has a relation to the debug status in the settings.py file.
for example;
If Debug:
alias = ''
else:
alias = 'picon/'
if this is possible, how would i then reference to the alias in the views.py using the following line;
return HttpResponseRedirect('/picon/customers')
Edit: added my urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
Any suggestions are much appreciated.
Regards.
This is exactly why you should not hard-code URLs in views or templates. Use django.core.urlresolvers.reverse - or the {% url %} template tag - to calculate the URL dynamically, including any prefix.
return HttpResponseRedirect(reverse('customers'))
where 'customers' is the name value from the URLconf.
You can include the urls from another file, dynamically:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
#PAY ATTENTION HERE
from another_location import urls as another_urls
admin.autodiscover()
if settings.DEBUG:
urlpatterns = url(r'^picon/', include(another_urls))
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
else:
urlpatterns = url(r'^', include(another_urls))
Having your another_location.urls file like this:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'signups.views.home', name='home'),
url(r'^thank-you/$', 'signups.views.thankyou', name='thankyou'),
url(r'^about-us/$', 'signups.views.aboutus', name='aboutus'),
url(r'^customers/$', 'formlist.views.customers', name='customers'),
url(r'^addcustomer/$', 'formlist.views.addcustomer', name='addcustomer'),
#url(r'^addcustomer_res/$', 'formlist.views.addcustomer', name='addcustomer'),
url(r'^admin/', include(admin.site.urls)),
)
You must ensure such file is pythonpath-reachable via imports.
Edit: also, don't hardcode URLS. Always use reverse. It will be harmless to you since you gave names for each of your urls (reverse('customers') will give you the right url no matter where is it deployed).