表单未验证两次击中网址失败

时间:2019-04-10 13:29:46

标签: django

我有一个用于输入产品详细信息的模型表格。提交后,我将在视图中验证此表单并返回另一个HTML页面。一切正常。

现在在另一页上,提交表单后我要去哪里,我正在做两件事: 1.单击刷新图标刷新页面:在这种情况下,验证将照常进行。

我在做第二件事时遇到问题:

  1. 当我再次点击相同的URL时,表单验证失败。

为什么?谁能帮我吗?

获取表格的网址:

 /createProduct

提交表单的网址:

/saveProduct

这是我的模型表格:

#Model form for Product model
class ProductForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'class':'form- 
control','placeholder':'Product Name','name':'product-name'})) 
category = forms.ModelChoiceField(widget=forms.Select(attrs={'class':'form-control','selected':'Select Product Category','name':'product-category'}),queryset=Category.objects.all())
description = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Product Description','name':'product-description'})) 

price =forms.DecimalField(widget=forms.NumberInput(attrs={'class':'form-control','placeholder':'Product Price','name':'product-price'}))
contact =forms.IntegerField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Provide your contact no','name':'poster-contact'}))



class Meta:
    model = Product
    #fields = ('name','category', 'image','description','price')
    fields = ('name','category','description','price','contact')

这是呈现产品表单的视图:

def create_product_fn(request):
 form = ProductForm()
 return render(request, 'olx/product/create_product.html', {'form': form})

这是提交表单后我要去的视图:

def  save_new_product(request):
 if request.user.is_authenticated:

   formToSave = ProductForm(request.POST,request.FILES)

   if formToSave.is_valid():

     # Instead of saving it store it in session to save it later 

     request.session['save_product_form_data_1'] = request.POST
     request.session['save_product_form_data_2'] = request.FILES

   else:
      # if form is not valid, returning to form to enter product detail again
      return create_product_fn(request,info)

   return render(request, 'olx/product/upload_product_photos.html')  
 else:
   print('session expired') 
   return render(request, 'olx/login.html')

0 个答案:

没有答案