2

Having trouble with a FormView during testing. I have the following test:

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)

The view has a decorator that checks if a user passes a test, if not, redirects. A simple user_passes_test function that has been tested and works on a different form/formview. The form has been tested and passes every test. The view in question:

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 just in case:

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"),
)

I'm getting, an error 405 as described in the title. Not sure how there can be a problem with the method. Any help?

user3084860
  • 421
  • 5
  • 19
  • duplicate of [405 error on django ajax POST](http://stackoverflow.com/questions/7819206/405-error-on-django-ajax-post) – Hedde van der Heide Aug 18 '14 at 21:09
  • 2
    Don't think that question answers mine though. Problem isn't a missing trailing slash because I'm using reverse() and append_slash is True. Not accidentally referring to another url by accident either. – user3084860 Aug 18 '14 at 21:13
  • your problem is the missing crsf token as also commented in that post – Hedde van der Heide Aug 18 '14 at 21:16
  • Well, I'm accessing the view from the Client. According to the docs: "By default, the test client will disable any CSRF checks performed by your site." So whether or not I have a CSRF token shouldn't matter. – user3084860 Aug 18 '14 at 21:19
  • 1
    @HeddevanderHeide Sigh, not only is this question not even about an ajax post (I'm not making an ajax post request, my formview just accepts ajax post requests), but that question still doesn't answer mine. I might have to reask this, because I don't see how this is a duplicate question. – user3084860 Aug 19 '14 at 12:18

0 Answers0