programming:django
This is an old revision of the document!
Create a project
django-admin startproject mysite
Launch the server
python manage.py runserver
Create a app inside a project
Everything are apps. Each app has it's own subfolder. To create a app sceleton run
python manage.py startapp polls
Register URL
In the polls/urls.py file include the following code:
- polls/urls.py
polls/urls.py¶ from django.urls import path from . import views urlpatterns = [ # 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'), ]
The corresponding view looks like this. The name correspond with the method name of views.py.
- polls/views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.") def detail(request, question_id): return HttpResponse("You're looking at question %s." % question_id) def results(request, question_id): response = "You're looking at the results of question %s." return HttpResponse(response % question_id) def vote(request, question_id): return HttpResponse("You're voting on question %s." % question_id)
Now we need to add this url to our project:
- mysite/urls.py
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
Templates
Templates are in the subfolder of the app. e.g. `mysite/polls/templates/*`
programming/django.1543226949.txt.gz · Last modified: by clemix
