Django ListView页面重定向到自定义URL

时间:2017-07-18 17:24:43

标签: django

我正在尝试在提交将对象ID放入urlpattern的帖子后重定向页面。目前它返回以下错误:

  

使用关键字参数'{'kwargs':{'pk':43}}'找不到'create_quotation_add_product'。尝试了1种模式:['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

此时两个网址工作正常,我无法将两者联系在一起。

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>

很抱歉,如果我使用的条款不正确,最近才开始学习。

2 个答案:

答案 0 :(得分:1)

尝试以下django的反向方法来获取URL

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

答案 1 :(得分:0)

根据redirect快捷方式,如果您想通过create_quotation_add_product快捷方式将任何参数传递给redirect网址,您应该这样做:

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

你这样做的方式是指Django的reverse内置函数。