翻譯|使用教程|編輯:董玉霞|2022-06-09 11:01:57.093|閱讀 27 次
概述:PyCharm官方正版下載 本篇PyCharm使用教程將介紹在創(chuàng)建Django項目時如何創(chuàng)建 Django 模板??的相關(guān)內(nèi)容?。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PyCharm最新版本V2022.1支持自定義包存儲庫的身份驗證,如果還沒下載,點擊下面下載。
本篇PyCharm使用教程將介紹在創(chuàng)建Django項目時如何創(chuàng)建 Django 模板??的相關(guān)內(nèi)容?。
如您所見,這些頁面的設計在視圖中是硬編碼的。因此,為了使其更具可讀性,您必須編輯相應的 Python 代碼。然后讓我們將輸出的可視化表示與 Python 分開 - 為此,讓我們創(chuàng)建模板。
打開以編輯文件polls/views.py并將其內(nèi)容替換為以下代碼:
from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from .models import Question, Choice def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context) def detail(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/detail.html', {'question': question}) def results(request, question_id): question = get_object_or_404(Question, pk=question_id) return render(request, 'polls/results.html', {'question': question}) def vote(request, question_id): question = get_object_or_404(Question, pk=question_id) try: selected_choice = question.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render(request, 'polls/detail.html', { 'question': question, 'error_message': "You didn't select a choice.", }) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
順便提一下,幫助您創(chuàng)建導入語句的導入助手。
您注意到的第一件事是對頁面index.html的未解決引用:
PyCharm 建議快速修復:如果單擊燈泡或按Alt+Enter,則會在模板文件夾中創(chuàng)建相應的模板文件(請注意,PyCharm 還會創(chuàng)建該模板應駐留的目錄polls ):
到目前為止,文件index.html是空的。向其中添加以下代碼:
{% load staticfiles %}{% if latest_question_list %}
No polls are available.
{% endif %}注意模板文件中的代碼完成!例如,當您鍵入開頭時{%,PyCharm 會自動添加匹配的結(jié)尾%},將插入符號放在將來輸入的位置。在 HTML 標記中,也可以使用代碼完成。
注意分別出現(xiàn)在文件views.py和index.htmlhtml類型中的圖標和圖標:
這些圖標使您可以立即在視圖方法及其模板之間跳轉(zhuǎn)。
JetBrains PyCharm是一種Python IDE,其帶有一整套可以幫助用戶在使用Python語言開發(fā)時提高其效率的工具。此外,該IDE提供了一些高級功能,以用于Django框架下的專業(yè)Web開發(fā)。
想要了解或購買PyCharm正版授權(quán)的朋友,歡迎咨詢
PyCharm技術(shù)交流群:786598704 歡迎進群一起討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn