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

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


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!


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


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 pathfrom . import viewsurlpatterns = [    # 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'),]