我创建了具有四个选项值(1,2,3,4)的选择标签。当我选择4并按提交时,它会更改回1.选择选项包含用户所需的产品数量。那么如何在按下提交按钮后保留选项值。我尝试过这样,在我按下提交按钮值更改回 1.Is 有什么方法可以克服这个问题吗?
我的模板文件,
<label for="quantity">Quantity</label>
<select id="quantity" name="quantity">
<option value="1" {% if quantity == '1' %}selected{% endif %}>1</option>
<option value="2" {% if quantity == '2' %}selected{% endif %}>2</option>
<option value="3" {% if quantity == '3' %}selected{% endif %}>3</option>
<option value="4" {% if quantity == '4' %}selected{% endif %}>4</option>
</select>
<input type="submit" value="Buy"/>
更新:forms.py,
class SortForm(forms.Form):
RELEVANCE_CHOICES = (
(1,'1'),(2, '2'),(3,'3'), (4,'4'),(5,'5'),
)
sort = forms.ChoiceField(choices = RELEVANCE_CHOICES,label='Quantity')
views.py,
from .forms import SortForm
@csrf_protect
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products}
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
"form": form
}
return render(request,"buy_book.html",context)
在模板文件中,我添加了这一行,
{{form.as_p}}
现在我得到空白输出。我认为表单在模板中无法识别。
这里的问题是您的模板只是显示数据,它对状态一无所知。因此,如果要实现这种行为,则需要从后端提供所有必需的数据。另外,正如@solarissmoke提到的,你应该使用django表单。
例如(下面的伪代码)
def my_view(request):
if request.method == 'POST':
form = MyForm(request.data)
if form.is_valid():
form.save()
redirect(reverse('myview'))
else:
form = MyForm(instance) # <- instance is object with previously saved data
return render(request, 'my_template.html' , {'form': form})
第二部分
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products}
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
"form": form # <- here is problem
}
return render(request,"buy_book.html",context)
您正在将表单添加到 if request.method == 'POST'
中的上下文中。它应该像这样
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products, 'form': form} # <- here
if request.POST.get('quantity'):
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = request.POST.get('quantity')
Quantity=int(u_quantity)
total = rate*Quantity
context = {
"products":my_products,
"Total":total,
}
return render(request,"buy_book.html",context)
在您看来,只有在发布数据中有数量时,您才会将表单添加到上下文数据中,无论查看需要它,您都应该将其添加到上下文中。
您还应该实际使用该表单,因此与其检查帖子数据,不如检查表单的有效性,然后使用其清理的数据。
def buy_book(request,pk):
form = SortForm(request.POST or None)
my_products = Add_prod.objects.filter(pk=pk)
#Add_prod is the model class name
context = {"products":my_products,
'form': form}
if form.is_valid():
for i in my_products:
rate= i.price
#price is the column name in the model class
u_quantity = form.cleaned_data.get('sort', 0)
Quantity=int(u_quantity)
total = rate*Quantity
context['total'] = total
return render(request,"buy_book.html",context)
"int"对象不可迭代
可能是因为您的排序字段不是元组列表
choices = [(i, i) for i in range(1,6)]