馃敟閭傞€匘jango-鍐呭鉁呯1閮ㄥ垎锛氥€愰亣瑙丏jango銆戔€斺€旓紙1锛夊垱寤洪」鐩渽绗?閮ㄥ垎锛氥€愰亣瑙丏jango銆戔€斺€旓紙2锛夋暟鎹簱閰嶇疆馃洜锔廝art3:[MeetDjango]-(3)View馃洜锔廝art4:[MeetDjango]-(4)Formandgeneralview馃洜锔廝art5:[MeetDjango]-(5)鏀硅繘鐣岄潰锛堣嚜瀹氫箟鐣岄潰鍜屾牱寮忥級馃洜锔忕鍏儴鍒嗭細銆愯璇咲jango銆戔€斺€旓紙鍏級鑷畾涔夌鐞嗙晫闈煕狅笍绗竷閮ㄥ垎锛氥€愯璇咲jango銆戔€斺€旓紙涓冿級鑷姩鍖栨祴璇曫煄堝墠瑷€鏈郴鍒楁枃绔犳槸鏍规嵁Django瀹樻柟鏂囨。鏁欑▼鐨勫熀纭€妯℃澘鍋氱殑涓€浜涙敼杩涘拰鍒犻櫎锛屽鍔犱簡涓€浜涙垜鑷繁鐨勮瑙c€傚笇鏈涘ぇ瀹剁湅瀹屾湰绯诲垪鏂囩珷鍚庯紝鑳藉瀵笵jango鏈変竴涓竻鏅扮殑璁よ瘑銆傝矾婕极鍏朵慨杩滃叜锛屾垜灏嗕笂涓嬫眰绱紒Django==瀹樻柟鏂囨。==锛歨ttps://www.djangoproject.com/鉂椻潡鉂楀湪瀛︿範杩囩▼涓紝澶氱湅瀹樻柟鏂囨。鍙互瑙e喅寰堝闂鉂椻潡鉂楁湰鏁欑▼浣跨敤璇楁瓕鏉ョ鐞嗛」鐩幆澧冦€傜浉鍏宠瘲璇嶇殑瀹夎鍜屼娇鐢ㄨ鍙傝€冦€怭ython-铏氭嫙鐜銆戦」鐩惎鍔紝浠庨殧绂诲紑鍙戠幆澧冨紑濮?SegmentFaultSifu椤圭洰鍦板潃锛歨ttps://github.com/CoderBerryRabbit/MeetDjango1.鏌ョ湅姒傝ViewsinDjango鐨勬蹇垫槸銆愬叿鏈夌浉鍚屽姛鑳藉拰妯℃澘鐨勭綉椤甸泦鍚堛€戙€備緥濡傦紝鍦ㄤ竴涓崥瀹㈠簲鐢ㄤ腑锛屽彲鑳戒細鍒涘缓濡備笅瑙嗗浘锛氬崥瀹㈤椤碘€斺€旀樉绀烘渶杩戠殑鍐呭鍐呭璇︽儏椤碘€斺€旇缁嗘樉绀烘煇涓唴瀹逛互骞翠负鍗曚綅鐨勫瓨妗i〉鈥斺€旀樉绀烘墍閫夊勾浠界殑姣忎釜鏈堝瓨妗i〉ofcreatedcontentinmonth-鏄剧ず鍦ㄦ墍閫夋湀浠界殑姣忎竴澶╁垱寤虹殑鍐呭Archivepageinunitofday-鏄剧ず鍦ㄦ墍閫夋棩鏈熷垱寤虹殑鎵€鏈夊唴瀹笴ommentProcessor-鐢ㄤ簬娣诲姞璇勮浠ュ搷搴斿唴瀹瑰湪鎶曠エ搴旂敤绋嬪簭涓紝闇€瑕佷互涓嬭鍥撅細闂绱㈠紩椤甸潰鈥斺€旀樉绀烘渶杩戠殑鍑犱釜鎶曠エ闂璇︽儏椤甸潰鈥斺€旀樉绀轰竴涓姇绁ㄩ棶棰樺拰涓€涓病鏈夌粨鏋滅殑閫夐」鍒楄〃鎶曠エ缁撴灉鎶曠エ澶勭悊绋嬪簭鈥斺€旂敤浜庡搷搴旂敤鎴峰闂鎶曠エ鐨勬搷浣滃湪Django涓紝缃戦〉鍜屽叾浠栧唴瀹归兘鏄粠瑙嗗浘涓淳鐢熷嚭鏉ョ殑銆傛瘡涓鍥句唬琛ㄤ竴涓狿ython鍑芥暟锛堟垨鑰咃紝瀵逛簬鍩轰簬绫荤殑瑙嗗浘锛屼唬琛ㄤ竴涓柟娉曪級銆侱jango浼氭牴鎹敤鎴疯姹傜殑URL閫夋嫨浣跨敤鍝釜瑙嗗浘銆備负浜嗗皢URL涓庤鍥剧浉鍏宠仈锛孌jango浣跨敤URLconfs杩涜閰嶇疆銆俇RLconf灏哢RL鏄犲皠鍒拌鍥俱€備簩銆佺紪鍐欒鍥?polls/views.pyfromdjango.shortcutsimportrenderfromdjango.httpimportHttpResponsedefindex(request):returnHttpResponse("Hello,world.You'reatthepollsindex.")defdetail(request,question_id):returnHttpResponse(f"You'relookingatquestion{question_id}")defresults(request,question_id):response="You'relookingattheresultsofquestion{}"returnHttpResponse(response.format(question_id))defvote(request,question_id):returnHttpResponse("You'revotingonquestion%s."%question_id)涓夈€佹坊鍔爑rl#polls/urls.pyfromdjango.urlsimportpathfrom銆俰mportviewsurlpatterns=[path("",views.index,name="index"),path("{%forquestioninlatest_question_list%}
{%endif%}#polls/templates/polls/detail.html{{question.question_text}}
{%forchoiceinquestion.choice_set.all%}
馃寠5.3浣跨敤妯℃澘#polls/views.pyfromdjango.shortcutsimportrenderfromdjango.httpimportHttpResponsefromdjango.templateimportloaderfrom.modelsimportQuestiondef绱㈠紩锛堣姹傦級锛歭atest_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))鍚姩Django鏈嶅姟鍣紝杈撳叆http://127.0.0.1:8000/polls/鏌ョ湅馃寠5.4浣跨敤妯℃澘锛堝揩鎹峰嚱鏁帮級锛歳ender()fromdjango.httpimportHttpResponsefromdjango.shortcuts浠嶥jango瀵煎叆娓叉煋銆傛ā鏉垮鍏ュ姞杞藉櫒浠?modelsimportQuestiondefindex(request):latest_question_list=Question.objects.order_by("-pub_date")[:5]context={"latest_question_list":latest_question_list}returnrender(request,"polls/index.html",context)浣跨敤render锛屾棤闇€瀵煎叆loader鍜孒ttpResponse銆備絾鏄鏋滆繕鏈夊叾浠栧姛鑳斤紙姣斿detail銆乺esults銆乿ote锛夐渶瑕佺敤鍒帮紝闇€瑕佷繚鐣橦ttpResponse鐨勫鍏ャ€傪煂?.5鎶涘嚭404閿欒Question.DoesNotExist:raiseHttp404("Questiondoesnotexist")returnrender(request,"polls/detail.html",{"question":question})鎵撳紑Django鏈嶅姟鍣紝杈撳叆http://127.0.0.1:8000/polls/1/鎴杊ttp://127.0.0.1:8000/polls/100/鏉ユ煡鐪嬨€備竴涓揩鎹峰嚱鏁帮細get_object_or_404()#polls/views.pyfromdjango.httpimportHttpResponse,Http404fromdjango.shortcutsimportrender,get_object_or_404fromdjango.templateimportloaderfrom.modelsimportQuestiondefdetail(request,question_id):question=get_object_or_404(Question,pk=question_id)returnrender(request,"polls/detail.html",{"question":question})鍏€佸彇鍑烘ā鏉夸腑纭紪鐮佺殑URL鍦╬olls/index.html涓‖缂栫爜閾炬帴锛?li>
