Python Console(2)Vesion Upgrade and Sample Admin/List/Detail

PythonConsole(2)VesionUpgradeandSampleAdmin/List/Detail

Creatinganadminuser

>pythonmanage.pycreatesuperuser

mydefaultusernameiscarl,andmyusuallypassword.

Makethepollappmodifiableintheadmin.

modifythepolls/admin.pyfile

#-*-coding:utf-8-*-from__future__importunicode_literals

fromdjango.contribimportadmin

#Registeryourmodelshere.from.modelsimportQuestionAdmin

from.modelsimportChoice

admin.site.register(Question)

admin.site.register(Choice)

ViewModuleandFunctions

Question‘index’Page,Question‘detail’page,Question‘results’page,Voteaction

preparemoreviewsinpolls/views.py

defdetail(requesst,question_id):

returnHttpResponse("You'relookingatquestion%s."%question_id)

defresults(request,question_id):

response="You'relookingattheresultsofquestion%s."returnHttpResponse(response%question_id)

defvote(request,question_id):

returnHttpResponse("You'revotingonquestion%s."%question_id)

urlmappinginpolls/urls.py

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"),

]

CalltheDAOAPIanddoingsomethingintheindexpage

defindex(request):

latest_question_list=Question.objects.order_by('-pub_date')[:5]

output=','.join([q.question_textforqinlatest_question_list])

returnHttpResponse(output)

UseTemplate

djangowilllookfortemplatesdirectoryindefault.polls/templates/polls/index.html

{%iflatest_question_list%}

<ul>

{%forquestioninlatest_question_list%}

<li>

<ahref="/polls/{{question.id}}/">

{{question.question_text}}

</a>

</li>

{%endfor%}

</ul>

{%else%}

<p>Nopollsareavailable.</p>

{%endif%}

Thetemplaterenderandloading

fromdjango.httpimportHttpResponse

fromdjango.templateimportloader

from.modelsimportQuestion

defindex(request):

latest_question_list=Question.objects.order_by('-pub_date')[:5]

template=loader.get_template('polls/index.html')

context={

'latest_question_list':latest_question_list,

}

returnHttpResponse(template.render(context,request))

Wecanuseshortcutrenderinsteadoftemplateloader

fromdjango.shortcutsimportrender

fromdjango.httpimportHttpResponse

from.modelsimportQuestion

defindex(request):

latest_question_list=Question.objects.order_by('-pub_date')[:5]

context={

'latest_question_list':latest_question_list,

}

returnrender(request,'polls/index.html',context)

Raisinga404ifnecessary

fromdjango.httpimportHttp404

defdetail(request,question_id):

try:

question=Question.objects.get(pk=question_id)

exceptQuestion.DoesNotExist:

raiseHttp404("Questiondoesnotexist")

returnrender(request,'polls/detail.html',{'question':question})

Ashortcut:get_object_or_404()

fromdjango.shortcutsimportget_object_or_404,render

defdetail(request,question_id):

question=get_object_or_404(Question,pk=question_id)

returnrender(request,'polls/detail.html',{'question':question})

InsidethetemplatewecangettheURLfromconfiguration

<li>

<ahref="{%url'detail'question.id%}">

{{question.question_text}}

</a>

</li>

References:

https://docs.djangoproject.com/en/1.11/intro/tutorial02/

https://docs.djangoproject.com/en/1.11/intro/tutorial03/

https://docs.djangoproject.com/en/1.11/intro/tutorial04/

https://docs.djangoproject.com/en/1.11/intro/tutorial05/

https://docs.djangoproject.com/en/1.11/intro/tutorial06/

https://docs.djangoproject.com/en/1.11/intro/tutorial07/

相关推荐