Django 02 urls.py

Copyright: arbitrary, seeding https://blog.csdn.net/qq_32662595/article/details/85272721!
  1. File Locations
    urls.py location
  2. 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/
	]

document content
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.

Official Documents

	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:
Tab bar

Click 'What's up' to enter into the details pageDetail

Note: All html files need to be placed polls files in the templates folder.
Django will automatically look for the file templates folder

  1. Throw 404
    from django.http import HttpResponse, Http404Here Insert Picture Description
    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

  2. 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

  3. 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: settingfile ROOT_URLCONFconfiguration is not modified

Here Insert Picture Description

GIT source address: https://gitee.com/UniQue006/django_mysite.git

Guess you like

Origin blog.csdn.net/qq_32662595/article/details/85272721