Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 369
jay Avatar asked Oct 16 '12 15:10

jay


People also ask

How do I change the root URL in Django?

A quick way is to include the projects url patterns at the new path. At the bottom of the main urls.py include the base_patterns at the new path.

How do I reference a URL in Django?

Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .


2 Answers

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!

like image 147
jay Avatar answered Oct 20 '22 06:10

jay


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

like image 31
jdcaballerov Avatar answered Oct 20 '22 04:10

jdcaballerov