我使用通價值爲{{form.Product_ID}}在HTML的Django
{{ form.Product_PId }}
,使這個領域
反正是有過這個值
{{ Product.PId }}
其中PId是來自另一個數據庫的主鍵
和是這樣
<input id="id_Product_ID" min="0" name="Product_ID" type="number" value="{{Product.PId}}" required="">
,使得最終結果將是象
<input id="id_Product_ID" min="0" name="Product_ID" type="number" value="1" required="">
**總結問題
是有辦法來傳遞{{Product.PId }}轉換爲{{form.Product_PId}} ,以便{{Product.PId}}的值可以變成從另一個數據庫取得的可用數字**
re ference
forms.py
class PlaceOrder(forms.ModelForm):
class Meta:
model = Order
fields = ["Product_ID","HowMany","DateSubmit",]
HTML
{% for Product in Display_Product %}
<form method="post" action="" id="CartInput"> {% csrf_token %}
<div>
{{ form.Product_ID }}
{{ form.HowMany }}
{{ form.DateSubmit }}
</div>
<div>
<input name="Cart{{ Product.PId }}" type="submit" value="Add to Cart">
</div>
</form>
{% endfor %}
views.py
class Catalogue(generic.ListView, ModelFormMixin):
template_name = 'Shop/Catalogue.html'
model = models.Order
form_class = forms.PlaceOrder
def get(self, request, *args, **kwargs):
Display_Product = Product.objects.all()
now = datetime.datetime.now()
*** tried this one but failed, it just pass in the string {{Product.PId}} and not turn into number ***
form = PlaceOrder(initial= {'Product_ID': "{{Product.PId}}"})
context = {
'form': form,
'date': now,
'Display_Product': Display_Product
}
return render(request, 'Shop/Catalogue.html', context)
感謝和抱歉的長期問題
好謝謝你的回答將在稍後嘗試,當我回到我的答案時,我會等待幾個更多的答案,然後我選擇正確的答案: –