对于该问题的解决方案,我感到茫然。从本质上讲,这很简单。由于用户填写并在我的网站上提交,因此我想使用此ProductCreateView类中的表单更新数据库。表单显示正确,用户可以提交数据,之后我在控制台中看到POST请求已成功完成。但是,此后,此发布数据没有任何反应,并且用户被重定向到首页。似乎POST数据无处可去。 form_valid和form_unvalid这两个功能均不显示任何内容,并且不会更新数据库。
views.py
class ProductCreateView(CreateView):
model = added_product
fields = ['product_name','product_price','product_url_end']
def form_valid(self,form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
print("Valid form!")
return HttpResponse('Hello')
def form_invalid(self,form):
print("invalid form!")
# This method is called when invalid form data has been POSTed.
return HttpResponse('Hello')
models.py
class added_product(models.Model):
product_name = models.CharField(max_length=200)
product_price = models.CharField(max_length=200)
product_url_end = models.CharField(max_length=500)
def get_absolute_url(self):
return reverse('product-create')
added_product_form.html
<form action="/bolcombot/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
代码是如此简单,以至于我对这里可能出错的地方不知所措。实际上,它在我做一些无关的事情之前和之后都奏效了。可能我间接地打破了这一点。任何人都可以指出我的解决方案方向吗?我将非常感激!