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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

Django:測試由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如何測試?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()天真地,我以為這會根據(jù)中發(fā)現(xiàn)的模式“喂養(yǎng)”相關(guān)內(nèi)容。但事實似乎并非如此。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'----------------------------------------------------------------------為什么會這樣呢?我設(shè)法通過和明確地解決了我的問題,但它看起來有點特別: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")在這里提到的不同選項中,哪一個更規(guī)范?我是否走錯了路,在定義時顯式使用變量?get_context_data()
查看完整描述

2 回答

?
一只甜甜圈

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

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


所以,它會像這樣:


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)一步說明:

  • 你的方法的定義對我來說似乎沒問題,get_context_data

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

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


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

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

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


當(dāng)您直接使用和調(diào)用視圖時,URL 處理程序不會運行,因此由您來傳遞 kwargs。setup()


def test_return_client_ordered_by_inscription_date(self): 

    view = BrowseClients()

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

    context = view.get_context_data()


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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