How to load urlconf route apps into projects in django 1.3


Django has special method to process URL and you can design the URL design however you want. To create URL design, you should create URLCONF modules. This modules will mapping url patterns to views. Before looking more, we should know about this basic things.

How Django processes a request ?

When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:

1. Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has an attribute called urlconf (set by middleware request processing), its value will be used in place of the ROOT_URLCONF setting.

2. Django loads that Python module and looks for the variable urlpatterns. This should be a Python list, in the format returned by the function django.conf.urls.defaults.patterns().

3. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

4. Once one of the regexes matches, Django imports and calls the given view, which is a simple Python function. The view gets passed an HttpRequest as its first argument and any values captured in the regex as remaining arguments.

5. If no regex matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.

Now we know how Django serve incoming requests. If we create a project with “django-admin.py startproject your-projects”, you will see urls.py inside the projects. This urls is URLCONF that will manage and route url pattern to views.

This is basic urls.py in projects folder :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns(”,
    # Examples:
    # url(r’^$’, ‘django_crawl.views.home’, name=’home’),
    # url(r’^django_crawl/’, include(‘django_crawl.foo.urls’)),
    # 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)),
)

URL in Django can define in two ways, in urls.py root of projects or inside apps. I will show you how to manage URLCONF in apps and projects. First, create apps called “posts” using “python manage.py startapp posts”. We will create URLCONF inside apps and will load it into projects. So, when you copy this apps into another projects, you just load it on the attached projects.

Inside posts apps, we creating urls.py.

1
2
3
4
5
6
from django.conf.urls.defaults import patterns

urlpatterns = patterns(
    ”,
    (r’^posts/$’, ‘posts.views.index’),
)

In case you need fast test, edit views.py :

1
2
3
4
from django.http import HttpResponse

def index(request):
        return HttpResponse("Hello world!")

Our purpose is when users accessing ‘http://localhost:8000/posts’ then it will show index views in posts apps. Is not ready yet, we need to load this apps URLCONF into project URLCONF. So, edit urls.py in root of projects and load this apps URLCONF by adding ” url(r”, include(‘posts.urls’)),” :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns(”,
    # Examples:
    # url(r’^$’, ‘django_crawl.views.home’, name=’home’),
    # url(r’^django_crawl/’, include(‘django_crawl.foo.urls’)),
    url(r”, include(‘posts.urls’)),
    # 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)),
)

Now, when you open “http://localhost:8000/posts”, then projects will load URLCONF posts app and redirect defined pattern to related views.

Also, great reference to read :
https://docs.djangoproject.com/en/1.3/topics/http/urls/


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.