假设我有以下网址
path('clients/by_<str:order>', BrowseClients.as_view(), name='browse_clients')
及其相应的视图
@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
如何测试?context
class 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()
天真地,我以为这会根据中发现的模式“喂养”相关内容。但事实似乎并非如此。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'
----------------------------------------------------------------------
为什么会这样呢?我设法通过和明确地解决了我的问题,但它看起来有点特别:statusorder
def 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")
在这里提到的不同选项中,哪一个更规范?我是否走错了路,在定义时显式使用变量?get_context_data()
一只甜甜圈
慕桂英546537
相关分类