在测试期间遇到FormView问题。我有以下测试:
def test_reviewform_contact(self):
data = {
'user': self.u2.pk,
'product': self.r1.pk,
'rating': 5,
'title': "I turn to see my dream",
'content': "This reward was joocy as FUARK."
}
response = self.client.post(reverse('reviews:reviewreward'), data)
location = "http://testserver%s?next=%s" % (reverse('affiliates:onboarding_contact'), reverse('reviews:reviewreward'))
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], location)
该视图有一个装饰器,用于检查用户是否通过测试,如果没有,则重定向。一个简单的user_passes_test函数,已经过测试,可以在不同的表单/表单视图上工作。表格已经过测试并通过了每项测试。有问题的观点:
class ReviewFormView(FormView):
template_name = "general/processing.html"
form_class = ReviewForm
success_url = reverse_lazy('offers:dashboard')
def render_to_json_response(self, context, **response_kwargs):
data = json.dumps(context)
response_kwargs['content_type'] = 'application/json'
return HttpResponse(data, **response_kwargs)
def form_invalid(self, form):
response = super(ReviewFormView, self).form_invalid(form)
if self.request.is_ajax():
t = "We're having some problems saving your review..."
m = form.errors
message = render_to_string('reviews/error-alert.html', {'title':t, 'messages':m})
data = { 'messages': message }
return self.render_to_json_response(data, status=400)
else:
return response
def form_valid(self, form):
response = super(ReviewFormView, self).form_valid(form)
review = form.save()
if self.request.is_ajax():
data = {
'pk': review.id,
'title': review.title,
}
return self.render_to_json_response(data, status=200)
else:
return response
@method_decorator(active_and_login_required)
def dispatch(self, *args, **kwargs):
return super(ReviewFormView, self).dispatch(*args, **kwargs)
Urlconf以防万一:
from django.conf.urls import patterns, include, url
from reviews.views import *
urlpatterns = patterns('',
url(r'^review-opinion/$', ReviewHelpfulView.as_view(), name="reviewlike"),
url(r'^review-reward/$', ReviewFormView.as_view(), name="reviewreward"),
)
我得到了,标题中描述的错误405。不确定该方法有什么问题。有什么帮助吗?