我試圖重新定向頁面後提交了一個對象標識放置到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>
很抱歉,如果我使用的術語是不正確的,最近纔開始學習。