查看返回无,而不是HttpResponse

时间:2016-01-14 17:54:52

标签: jquery ajax django django-templates django-views

当我尝试使用ajax和基于类的视图发布表单时,我收到上述错误 这是模板

<form method="post" id="guestform">
                   {% csrf_token %}  
                    {% load i18n %}

                      <div id="div_id_username" class="form-group">
                        <div class="controls "> 
                          <input class="textinput form-control" id="id_email" maxlength="254" name="email" type="text" placeholder="Enter Email" /> 
                        </div> 
                      </div> 

                      <input class='btn btn-block btn-primary nextBtn' type="submit" value="Continue As Guest" onclick="ContinueAsGuest(event)" />
               </form>

这是脚本

<script type="text/javascript">
          function ContinueAsGuest(event) {
            var email = $("#id_email").val();
            $.ajax({        
                type: "POST",
                data : {action : 'guest' , email : email , csrfmiddlewaretoken: "{{ csrf_token }}"},
                url: "{% url 'checkout' %}",
                success: function(data) {
                  $("#loading-image").hide();

                },
                error: function(response, error) {
                    alert(error);  
                }
            });
    }
</script>

这是基于类视图的视图

class CheckoutView(CartOrderMixin , DetailView , FormView):
    model = Cart
    template_name = "orders/checkout_view.html"

    def get_object(self , *args , **kwargs):
        cart = self.get_cart()

        if cart == None:
            return None

        return cart 

    def get_context_data(self ,*args , **kwargs):
        context = super(CheckoutView , self).get_context_data(*args , **kwargs)
        user_can_continue = False

        user_checkout_id = self.request.session.get('user_checkout_id')

        if self.request.user.is_authenticated():
            user_checkout , created = UserCheckout.objects.get_or_create(email=self.request.user.email)
            user_checkout.user = self.request.user
            user_checkout.save()
            self.request.session['user_checkout_id'] = user_checkout.id
            context["client_token"] = user_checkout.get_client_token()

        if not self.request.user.is_authenticated() and user_checkout_id == None:
            context["login_form"] = AuthenticationForm()
            context["next_url"] =  self.request.build_absolute_uri()

        else:
            pass

        if user_checkout_id != None:
            user_can_continue = True
            if not self.request.user.is_authenticated():
                user_checkout_2 = UserCheckout.objects.get(id=user_checkout_id)
                context["client_token"] = user_checkout_2.get_client_token()

        context["user_can_continue"] = user_can_continue
        context["form"] = GuestCheckoutForm()
        return context

    def post(self , request , *args , **kwargs):
        self.object = self.get_object()
        action = request.POST.get('action')
        print action
        email = request.POST.get('email')
        print email
        user_checkout , created = UserCheckout.objects.get_or_create(email=email)
        print user_checkout
        self.request.session['user_checkout_id'] = user_checkout.id

        if created:
            user_checkout.save()

    def get_success_url(self):
        return reverse('checkout')

    def get(self , request , *args , **kwargs):
        get_data = super(CheckoutView , self).get(request , *args , **kwargs)
        cart = self.get_object()

        if cart == None:
            return HttpResponseRedirect(reverse('cart'))

        new_order = self.get_order()

        user_checkout_id = request.session.get('user_checkout_id')

        if user_checkout_id != None:
            user_checkout = UserCheckout.objects.get(id=user_checkout_id)

            address = self.request.session.get('address_id')

            if request.is_ajax():
                if request.GET.get('action') == 'address':
                    if new_order.address == None:
                        user_checkout_id = self.request.session.get('user_checkout_id')
                        user_checkout = UserCheckout.objects.get(id=user_checkout_id)
                        address = UserAddress.objects.filter(user = user_checkout)
                        print address
                        return HttpResponseRedirect(reverse('useraddress'))

                if request.GET.get('action') == 'order':
                    data = {}
                    data["order"] = self.get_order()
                    return render(request , "orders/new_order.html" , data)


            new_order.user = user_checkout
            new_order.save()

        return get_data

我在这里出错了?我怎样才能解决上述错误。需要帮助。谢谢你

1 个答案:

答案 0 :(得分:4)

确保post()返回HttpResponse

from django.shortcuts import redirect

def post(self , request , *args , **kwargs):
    self.object = self.get_object()
    action = request.POST.get('action')
    print action
    email = request.POST.get('email')
    print email
    user_checkout , created = UserCheckout.objects.get_or_create(email=email)
    print user_checkout
    self.request.session['user_checkout_id'] = user_checkout.id

    if created:
            user_checkout.save()

    return redirect(self.get_success_url())

每个Django视图必须是callable,它接收HttpRequest以及可选参数,并返回HttpResponse个对象。 CBV不是例外,因此他们的getpost等视图必须返回回复。

虽然恕我直言,你应该继承generic.FormView - 它处理的不仅仅是成功的网址重定向。