In Django, How do you write the url pattern for '/' and other root-based urls
Asked Answered
I

4

23

I'm new to django, and one of the things that I'm still learning is url_patterns. I set up a pages app to handle the root path (http://www.mysite.com) as well as some static pages such as the about page. I figured out how to set up the url pattern for the root path, but I can't get the site to direct the path '/about' to the pages "about" view.

Here is my main urls.py

 from django.conf.urls import patterns, include, url
 from django.conf import settings
 urlpatterns = patterns('',
     url(r'^polls/', include('polls.urls')),
     url(r'^$', 'pages.views.root'),
     url(r'^/', include('pages.urls')),
  )

here is my pages urls.py

 from django.conf.urls import patterns, include, url
 urlpatterns = patterns('pages.views',
      url(r'^about', 'about'),
 )

Here is my pages views.py

 # Create your views here.
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.http import HttpResponse, HttpResponseRedirect
 from django.core.urlresolvers import reverse

 def root(request):
    return render_to_response('pages/root.html',context_instance=RequestContext(request))
 def about(request):
    return render_to_response('pages/about.html',context_instance=RequestContext(request))

If I change the main urls.py file to have r'^a/', include('pages.urls') then the path '/a/about' directs to the about action.. so I think it has to be an issue in the way i'm writing the url pattern in this file. But, I can't figure out how to change it. Can anyone help?

Illinois answered 16/10, 2012 at 15:43 Comment(1)
to the people voting to close this question, would you mind being more constructive and comment to let me know what guideline's are not being met, and/or how i could edit this question to better satisfy these requirements?Illinois
I
35

Figured out what the issue is. The proper url_pattern on the project level is:

 urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^$', 'pages.views.root'),
    url(r'', include('pages.urls')),
 )

When this is in place, '/about' and other simple paths direct properly.

Thanks everyone!

Illinois answered 16/10, 2012 at 16:3 Comment(2)
I would like to add for anyone else who missed it: pages is an application in OP's project, not a Django-specific syntax. It could have been other_pages or something similar.Yanirayank
This! When I first created my project, I mistakenly interpreted the project level url_pattern as an app in itself. It seems the correct design is to have a dedicated app for all web page content as well as any other specific apps. Is this correct?Schism
A
7

Try this, for url.py on the project level:

urlpatterns = patterns('',
# Examples:
url(r'^$', 'apps_name.views.home', name='home'),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),

(r'^about/', include('about.urls')),
)

and then the url.py for the app about

urlpatterns = patterns('',
    url(r'^$', direct_to_template, {"template": "about/about.html"}, name="about"),
)

Take into account that regular expression are evaluated from top to bottom, then if the path fits the regexp it will enter. To learn more about regexp google it or try the great book from Zed Shaw about regexps

Axle answered 16/10, 2012 at 15:51 Comment(0)
G
4

Note that from Django version 2.0 the URL pattern has changed to use django.urls.path() Check Example here: link

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
Gaea answered 8/12, 2017 at 12:52 Comment(0)
G
0

About the url method:

url(r'^$', 'pages.views.root')

url is deprecated in Django 3.1, it's good to use re_path instead.

https://docs.djangoproject.com/en/3.1/ref/urls/#s-url

https://docs.djangoproject.com/en/3.1/ref/urls/#re-path

Note: The r'^$'pattern WON'T work with path function, and will give you a misleading error that the route could not be found.

You have to use re_path(r'^$', [etc]) every time you use a regular expression instead of a simple string in pattern.

Giannagianni answered 17/3, 2021 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.