2016-01-14 62 views
0

我收到的時候我試圖使用Ajax和基於類的意見 這裏一起發佈形式上面的錯誤是模板視圖返回無替代的HttpResponse

<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 

其中視圖我我在這裏錯了嗎?我該如何解決上述error.Need help.Thank您

回答

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的視圖必須是與可選參數,並返回沿着接收HttpRequest一個callable一個HttpResponse對象。 CBV不是例外,所以他們的get,post等視圖必須返回響應。

雖然恕我直言,你應該繼承generic.FormView - 它處理的不僅僅是成功的URL重定向。

+0

獲得'超級'對象沒有任何屬性'發佈' –

+0

您有兩種選擇:從FormView繼承(我個人推薦的方式),或手動進行響應。我將用最後一個更新答案,以使您的代碼無需在其他地方打破工作。 –

+0

但需要在這個視圖中呈現多個表單。我猜表單視圖只接受一種表單。 –

相關問題