2017-07-18 102 views
0

我試圖重新定向頁面後提交了一個對象標識放置到urlpattern。目前,它返回以下錯誤:Django ListView頁面重定向到自定義URL

Reverse for 'create_quotation_add_product' with keyword arguments '{'kwargs': {'pk': 43}}' not found. 1 pattern(s) tried: ['create_quotation/quotation-id-(?P\d+)/$']

第一種觀點是用戶輸入的信息,並將其提交到數據庫中。第二個視圖需要將數據帶入url以調出提交的對象的主鍵,以用作單獨表中的外鍵。

Views.py

class CreateQuotation(ListView, ModelFormMixin): 
    model = Quote 
    form_class = QuoteForm 
    template_name='quotations/create_quotation.html' 

    def get(self, request, *args, **kwargs): 
     self.object = None 
     self.form = self.get_form(self.form_class) 
     return ListView.get(self, request, *args, **kwargs) 

    def post(self, request, *args, **kwargs): 
     self.object = None 
     self.form = self.get_form(self.form_class) 

     if self.form.is_valid(): 
      self.object = self.form.save(commit=False) 
      self.object.created_by = request.user 
      self.object.created_date = timezone.now() 
      self.object.save() 

      return redirect('create_quotation_add_product', kwargs={'pk': self.object.pk}) 

     return self.get(request, *args, **kwargs) 

class QuotationAddProduct(ListView, ModelFormMixin): 
    model = Quote_Products 
    form_class = QuoteProductForm 
    template_name='quotations/quotation_add_products.html' 

    def get(self, request, *args, **kwargs): 
     self.object = None 
     self.form = self.get_form(self.form_class) 
     return ListView.get(self, request, *args, **kwargs) 

    def post(self, request, *args, **kwargs): 
     self.object = None 
     self.form = self.get_form(self.form_class) 

     if self.form.is_valid(): 
      self.object = self.form.save(commit=False) 
      self.object.quote_id = Quote.objects.get(pk=self.kwargs['pk']) 
      self.object.save() 

      self.form = self.get_form(self.form_class) 

     return self.get(request, *args, **kwargs) 

    def get_context_data(self, *args, **kwargs): 
     context = super(QuotationAddProduct, self).get_context_data(*args, **kwargs) 

     context['form'] = self.form 
     context['quote_pk'] = Quote.objects.get(pk=self.kwargs['pk']) 
     context['quote_products'] = Quote_Products.objects.filter(
      quote_id=Quote.objects.get(pk=self.kwargs['pk']) 
     ) 

     return context 

兩個URL此刻做工精細,我在鏈接兩人在一起的麻煩。

urls.py

urlpatterns = [ 
    url(r'^create_quotation/$', CreateQuotation.as_view(), name='create_quotation'), 
    url(r'^create_quotation/quotation-id-(?P<pk>\d+)/$', 
    QuotationAddProduct.as_view(), 
    name='create_quotation_add_product' 
    ), 
] 

有什麼事我不是在HTML表單做是否正確?

報價/ create_quotation.html

<form action="" method="POST"> 
     {% csrf_token %} 

     {{ form.as_p }} 

     <input type='submit' value='CONFIRM DETAILS'/> 
</form> 

很抱歉,如果我使用的術語是不正確的,最近纔開始學習。

回答

0

下面django的反向方法嘗試URL

from django.urls import reverse 
from django.http import HttpResponseRedirect 
return HttpResponseRedirect(reverse('create_quotation_add_product', args=(self.object.pk,))) 
0

按照redirect快捷方式,如果你想通過redirect快捷的方法來傳遞任何參數到create_quotation_add_product網址,你應該這樣做是這樣的:

return redirect('create_quotation_add_product', pk=self.object.pk, another_key=another_value). 

你做的方式,是指reverse Django的內置函數。