2017-07-18 100 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的内置函数。