- File Locations
- Contents and documents
from django.conf.urls import url
from polls import views
urlpatterns = [ # path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'), # ex: /polls/
url(r'^polls/(?P<question_id>\d+)/$', views.detail, name='detail'), # ex: /polls/5/
url(r'^polls/(?P<question_id>\d+)/results/$', views.results, name='results'), # ex: /polls/5/results/
url(r'^polls/(?P<question_id>\d+)/vote/$', views.vote, name='vote'), # ex: /polls/5/vote/
]
The role of the file when the page is accessed, and a route map. (Request path mapping management)
written ##### official website document
access project will increase the current 404, the interface can not be found.
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
as follows:
Click 'What's up' to enter into the details page
Note: All html files need to be placed polls files in the templates folder.
Django will automatically look for the file templates folder
-
Throw 404
from django.http import HttpResponse, Http404
or
from django.shortcuts import get_object_or_404, render question = get_object_or_404(Question, pk=question_id)
get_object_or_404 () function to Django model as its first argument and an arbitrary number of keyword arguments that get through the management function of the model (). It leads Http404 if the object does not exist.
get_list_or_404 () function, as get_object_or_404 () - except using filter () instead of get (). It presents Http404 If the list is empty
-
Template (template)
<h1>{{ question.question_text }}</h1> <ul> {% for choice in question.choice_set.all %} <li>{{ choice.choice_text }}</li> {% endfor %} </ul>
Template system uses dot-lookup syntax to access variable attributes. {{Question.question}}, a first question on the object dictionary lookup. Does not exist, try to find a property - works in this case. If attribute lookup fails, it will always list-index lookup
-
Removing hard-coded
to use{% url %}
instead of the original hard-coded paths<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
Problem Cause above url can not be found: setting
file ROOT_URLCONF
configuration is not modified
GIT source address: https://gitee.com/UniQue006/django_mysite.git