第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Django:測(cè)試由url模式觸發(fā)的基于TemplateView的視圖?

Django:測(cè)試由url模式觸發(fā)的基于TemplateView的視圖?

慕虎7371278 2022-09-06 18:01:39
假設(shè)我有以下網(wǎng)址path('clients/by_<str:order>', BrowseClients.as_view(), name='browse_clients')及其相應(yīng)的視圖@method_decorator(login_required, name='dispatch')class BrowseClients(TemplateView):    template_name = "console/browse_clients.html"    def get_context_data(self, **kwargs):        context = super().get_context_data(**kwargs)        context['clients'] = Client.objects.filter(            owner=self.request.user.id).order_by(self.kwargs["order"])        context['form'] = AddClientForm()        return context如何測(cè)試?contextclass TestBrowseClientsView(TestCase, GeneralViewTest):    fixtures = ['users.yaml', 'clients.yaml']    def setUp(self):        self.request = RequestFactory().get('/console/clients/by_inscription')        self.request.user = User.objects.get(pk=1)    def test_return_client_ordered_by_inscription_date(self):         view = BrowseClients()        view.setup(self.request)        context = view.get_context_data()天真地,我以為這會(huì)根據(jù)中發(fā)現(xiàn)的模式“喂養(yǎng)”相關(guān)內(nèi)容。但事實(shí)似乎并非如此。view.setup(self.request).get_context_data()kwargsself.request======================================================================ERROR: test_return_client_ordered_by_inscription_date (console.tests.TestBrowseClientsView)----------------------------------------------------------------------Traceback (most recent call last):  File "/usr/src/jengu/console/tests.py", line 164, in test_return_client_ordered_by_inscription_date    context = view.get_context_data()  File "/usr/src/jengu/console/views.py", line 34, in get_context_data    owner=self.request.user.id).order_by(self.kwargs["order"])KeyError: 'order'----------------------------------------------------------------------為什么會(huì)這樣呢?我設(shè)法通過(guò)和明確地解決了我的問(wèn)題,但它看起來(lái)有點(diǎn)特別:statusorderdef get_context_data(self, status, order, **kwargs):    def test_return_clients_ordered_by_parameter(self):         view = BrowseClients()        view.setup(self.request)        context = view.get_context_data("all", "inscription")在這里提到的不同選項(xiàng)中,哪一個(gè)更規(guī)范?我是否走錯(cuò)了路,在定義時(shí)顯式使用變量?get_context_data()
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果要檢查響應(yīng)上下文中的內(nèi)容,首先需要使用響應(yīng)對(duì)象(而您不是,您只是在創(chuàng)建視圖的實(shí)例,而不是獲取視圖生成的響應(yīng))。我不知道,但我相信你會(huì)找到如何使我的答案適應(yīng)你的用例。RequestFactory


所以,它會(huì)像這樣:


def test_your_context(self):

    user = User.objects.get(pk=1)

    self.client.force_login(user) # because of the login_required decorator

    response = self.client.get(reverse("browse_clients"))

    assert response.context['your_context_key'] == "Anything you want to check"

只需做幾件事就可以進(jìn)一步說(shuō)明:

  • 你的方法的定義對(duì)我來(lái)說(shuō)似乎沒(méi)問(wèn)題,get_context_data

  • 如果您使用基于類(lèi)的視圖,如果您要檢查用戶是否登錄,我會(huì)建議您也使用基本視圖(LoginRequiredMixin)

  • 你給你的網(wǎng)址起了個(gè)名字,所以只是使用它,而不是寫(xiě)它的原始形式(這就是我在答案中所做的)。


查看完整回答
反對(duì) 回復(fù) 2022-09-06
?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

如果使用測(cè)試客戶端,它將負(fù)責(zé)運(yùn)行中間件和初始化視圖。


當(dāng)您直接使用和調(diào)用視圖時(shí),URL 處理程序不會(huì)運(yùn)行,因此由您來(lái)傳遞 kwargs。setup()


def test_return_client_ordered_by_inscription_date(self): 

    view = BrowseClients()

    view.setup(self.request, order='inscription')

    context = view.get_context_data()


查看完整回答
反對(duì) 回復(fù) 2022-09-06
  • 2 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)