Part 3: URLs, Python View

Original: http://docs.djangoproject.com/en/dev/intro/tutorial03/

Design your URLs

urls.py:

(regular expression, Python callback function [, optional dictionary])

Falls Regex passt wurd die Python Funktion aufgerufen und erhält als ersten Paramter einen request vom Typ HttpRequest

Edit mysite/urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^polls/$', 'mysite.polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
)

Die URL “/polls/23/” wird aufgelöst zu:

detail(request=<HttpRequest object>, poll_id='23')

poll_id='23' kommt von (?P<poll_id>\d+).

Write your first view

Datei mysite/polls/views.py:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

Write views that actually do something

...

Use the template system

...

Previous topic

Next topic