Я застрял, пытаясь проверить форму ChoiceField
в Django.Как проверить Django Form ChoiceField
У меня есть Form
с одним ChoiceField
:
class PickPlanForm(forms.Form):
"Set the `plan` session cookie for choice here."
plan_choices = Plan.objects.get_choices()
# Field
plan = forms.ChoiceField(required=True, choices=plan_choices)
Это список кортежей моего plan_choices
:
[('Bronze', 'Bronze ($10.00 per month)'),
('Silver', 'Silver ($20.00 per month)')]
Я пытаюсь проверить это следующим образом:
response = self.client.post(reverse('payment:register_step3'),
{'plan': 'Bronze'}, follow=True)
self.assertRedirects(response, reverse('payment:register_step4'))
Однако при выполнении моих тестов I ke ер получение отслеживающих ошибок:
Traceback (most recent call last):
File "/Users/aaron/Documents/djcode/textress_concierge/textress/main/tests/test_views.py", line 170, in test_register_step3
self.assertRedirects(response, reverse('payment:register_step4'))
File "/Users/aaron/Documents/virtualenvs/textress/lib/python3.4/site-packages/django/test/testcases.py", line 263, in assertRedirects
(response.status_code, status_code))
AssertionError: False is not True : Response didn't redirect as expected: Response code was 200 (expected 302)
Я использую:
Django 1.6.8
Python 3.4
Я думаю, это что-то просто, что мне не хватает?
спасибо
Edit: добавить Просмотреть
from django.views.generic import FormView
from braces.views import LoginRequiredMixin
class PickPlanView(LoginRequiredMixin, FormView):
"""
Step #3 of Registration
Pick a Plan, and save the Plan as a `session cookie` before creating
the Stipe Customer/Subscription using the Plan Choice.
"""
template_name = 'main/hotel_form.html'
form_class = PickPlanForm
success_url = reverse_lazy('payment:register_step4')
authenticated_redirect_url = settings.VERIFY_LOGOUT_URL
def get_form_kwargs(self):
"""Add the `request` Obj to Form, so I can set the PlanPick as a
session cookie for the time being."""
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
Заметки о Вид:
Он просто принимает форму выбора.
Это работает, когда я тестирую его вручную.
У меня возникли проблемы с участком Django ChoiceField Test
, потому что я раньше не тестировал ChoiceField
.
Спасибо
Не видя своего взгляда, очень сложно сказать, почему ваш тест терпит неудачу. – Brandon
Где/как именно вы перенаправляетесь на '' payment: register_step4''? Возникает ли переадресация из обработчика POST '' payment: register_step3''? – sthzg
@Brandon Я добавил свой вид. Спасибо за помощь. Пожалуйста, дайте мне знать. В основном это работает при тестировании вручную, что и заставляет меня думать, что это синтаксическая ошибка ChoiceField Test или неправильная комбинация '{'choice': 'value'}'. Спасибо –