Two types:

1. function object (need import view function)

2. string (don’t need to import)

——————

from django.conf.urls.defaults import *

from mysite.views import hi

urlpatterns = patterns(‘ ‘,

(r’^hi/$’, hi),

(r’^hello/$’, ‘mysite.views.hello’),

)

——–

Note in the first example,

hi

is a function object. This means you need to import the view function.

In second example,

‘mysite.views.hello’

is a string.

Here you don’t need to import it (Django will figure out)

Yes.

Under the project root file,

there is a urls.py

However, each application directory also has its own urls.py.

For example,

(r’^account/’, include(‘account.urls’)),

looks for urls.py file under account application directory.

Under account app directory, there is a urls.py including

url(r’^login/$’, auth_views.login, {‘template_name’: ‘account/login.html’}, name=’auth_login’),

then, /account/login/ url

will be mapped to

account/login.html.

Usually in django,

urls.py provides mapping between a url and a python function (that is, when a user clicks the url, the python function will be executed.)

The python function is usually defined at views.py

——

In a simple word, with direct_to_template, we don’t need views.py. It directly tells which page to go.

E.g.,

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

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

(r’^', include(‘main.urls’))

means

main app (main directory) has urls.py

* models.py  (under app)

database, basic element setup

* urls.py (under project)

connection between url and module in views.py

* views.py (under app)

from urls.py,

import models and pass data to template

* templates (html)

dynamic rendering from views.py performed

* settings.py (under project)

DB setting, app setting, directory setting

It automatically shows the Group, User, Sites in admin interface.

© 2011 Web Oom Suffusion theme by Sayontan Sinha
Switch to our mobile site
Top Footer