翻譯|使用教程|編輯:董玉霞|2022-03-31 13:48:44.200|閱讀 246 次
概述:本篇文章將介紹在PyCharm 中,如何去創(chuàng)建并運(yùn)行您的第一個 Django 項(xiàng)目的第四部分。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
本篇文章將介紹在PyCharm 中,如何去創(chuàng)建并運(yùn)行您的第一個 Django 項(xiàng)目的第四部分:
如圖所見,這些頁面的設(shè)計在視圖中是硬編碼的。因此,要使它更可讀,必須編輯相應(yīng)的Python代碼。然后,將輸出的Visual表分開。創(chuàng)建模板:
打開用于編輯文件輪詢/視圖。使用以下代碼替換其內(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,)))
對頁面index.html的未解決引用:
PyCharm 建議快速修復(fù):如果單擊燈泡或按Alt+Enter,則會在模板文件夾中創(chuàng)建相應(yīng)的模板文件(請注意,PyCharm 還會創(chuàng)建該模板應(yīng)駐留的目錄polls ):
到目前為止,文件index.html是空的。向其中添加以下代碼:
{% load staticfiles %}{% if latest_question_list %}
No polls are available.
{% endif %}對于 Django 3 及更高版本,您需要{% load static %}使用{% load staticfiles %}.
注意模板文件中的代碼完成!例如,當(dāng)您鍵入開頭時{%,PyCharm 會自動添加匹配的結(jié)尾%},將插入符號放在將來輸入的位置。在 HTML 標(biāo)記中,也可以使用代碼完成。
注意分別出現(xiàn)在文件views.py和index.htmlhtml類型中的圖標(biāo)和圖標(biāo):
這些圖標(biāo)使您可以立即在視圖方法及其模板之間跳轉(zhuǎn)。
正如您在視圖文件index.html中看到的,有一個對樣式表的引用,但未解決:
通過以下方式解決此引用:
1.創(chuàng)建目錄。為此,在項(xiàng)目視圖中,選擇 Python 包polls,然后按Alt+Insert。在出現(xiàn)的彈出菜單上,選擇Directory,并指定目錄結(jié)構(gòu)的名稱static/polls。
接下來,在這個目錄中創(chuàng)建一個樣式表。為此,請選擇最里面的目錄polls,按Alt+Insert,選擇選項(xiàng)Stylesheet,然后在打開的對話框中輸入樣式。
根據(jù)您的喜好,為創(chuàng)建的樣式表提供一些內(nèi)容。例如,我們希望看到綠色問題的項(xiàng)目符號列表:
li a { color: green; }
現(xiàn)在檢查可用民意調(diào)查的列表,管理站點(diǎn)已經(jīng)在運(yùn)行,訪問包含投票列表的頁面(索引頁面)的最簡單方法是指定其 URL:在瀏覽器的地址欄中,而不是/admin/,鍵入/polls / :
現(xiàn)在看看 PyCharm 如何幫助簡化應(yīng)用程序的測試。
polls目錄中已經(jīng)有文件tests.py 。到目前為止,這個文件是空的。自然,建議將新測試放在這個特定文件中。例如,我們想確保我們的投票不為空:
import datetime from django.urls import reverse from django.test import TestCase from django.utils import timezone from .models import Question def create_question(question_text, days): time = timezone.now() + datetime.timedelta(days=days) return Question.objects.create(question_text=question_text, pub_date=time) class QuestionViewTests(TestCase): def test_index_view_with_no_questions(self): """ If no questions exist, an appropriate message should be displayed. """ response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200) self.assertContains(response, "No polls are available.") self.assertQuerysetEqual(response.context['latest_question_list'], [])
要運(yùn)行此測試,請在編輯器中右鍵單擊文件tests.py的背景,選擇Run選項(xiàng),或者只需按Ctrl+Shift+F10。PyCharm 建議兩個選項(xiàng):運(yùn)行 UnitTest(定義為默認(rèn)測試運(yùn)行程序)或 Django 測試。
測試結(jié)果顯示在Run工具窗口的Test Runner選項(xiàng)卡中:
以上就是創(chuàng)建并運(yùn)行您的第一個 Django 項(xiàng)目第四部分的相關(guān)內(nèi)容。想要了解更多PyCharm使用教程。
JetBrains PyCharm是一種Python IDE,其帶有一整套可以幫助用戶在使用Python語言開發(fā)時提高其效率的工具。此外,該IDE提供了一些高級功能,以用于Django框架下的專業(yè)Web開發(fā)。
想要了解或購買PyCharm正版授權(quán)的朋友,歡迎咨詢
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn