pythonregexdjangourlconf

How Do I Properly Set Up URLConf in Django (RegEx?)


I've recently completed the Django Tutorial and am now working on my own web app. The problem I am having is setting up the URLConf for this application. I still don't fully understand the RegEx matching in order to link different pages to each other (or maybe I'm just forgetting something simple??)

I'm trying to setup the URLConf in a way such that when I click a button on each page, it will travel to the next page (there are 5 total).

Here is what it should look like

Page 0 (http://127.0.0.1:8000/)

Page 1 (http://127.0.0.1:8000/page1/)

And continue in this pattern (http://127.0.0.1:8000/page2/, http://127.0.0.1:8000/page3/, http://127.0.0.1:8000/page4/)

When I click Next Page on Page 0, it goes to Page 1. When I click Submit on Page 1, the URL slug page1 changes to page2 but still displays the same html page.

Here are my two urls.py files:

"""qfdjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

# builds URLS for all across the site
# decoupled from mainsite.urls
urlpatterns = patterns('',
    url(r'^',                include('mainsite.urls')),
    url(r'^someOtherPage/',  include('mainsite.urls')),
    url(r'^anotherPage/',    include('mainsite.urls')),
    url(r'^page1/',          include('mainsite.urls')),
    url(r'^page2/',          include('mainsite.urls')),
    url(r'^page3/',          include('mainsite.urls')),
    url(r'^page4/',          include('mainsite.urls')),
    url(r'^admin/',          include(admin.site.urls)),
)    

"""qfdjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import patterns, include, url

# these patterns are for specific sections of the site
# the 'primary' URLs are located in qfdjango.urls
# URL design of an app is specific to the app,
# not the whole Django project
urlpatterns = patterns('mainsite.views',
     url(r'^$', 'index'),
     url(r'$',  'page_1'),
     url(r'$',  'page_2'),
     url(r'$',  'page_3'),
     url(r'$',  'page_4'),
)

Solution

  • It seems you have defined the urls in a wrong way.

    It should be something like:

    from django.conf.urls import patterns, include, url
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^',                include('mainsite.urls')), # include your app urls once
        url(r'^admin/',          include(admin.site.urls)),
    )  
    

    Then in app's urls.py file, urls should be defined like:

    from django.conf.urls import patterns, include, url
    
    urlpatterns = patterns('mainsite.views',
        url(r'^$', 'index'),
        url(r'^someOtherPage/$', 'someOtherPage'),
        url(r'^anotherPage/$', 'anotherPage'),
        url(r'page1/$', 'page_1'), # page 1 url
        url(r'page2/$', 'page_2'), # page 2 url
        url(r'page3/$', 'page_3'), # page 3 url
        url(r'page4/$', 'page_4'), # page 4 url
    )