1👍
✅
The way your creating the articleForm is wrong.
It is better to use model forms like {{ form.as_p }}
or something like this.
But if you really want to create custom forms in html, you should use django’s standard id
and name
tags. If field name is blog_article
, corresponding html field’s id should be id_blog_article
. And the name attribute should be blog_article
. You have name attribute as article
not blog_article
. So here is correct article element in html form:
<label for="id_blog_article"> Article: </label>
<textarea id="id_blog_article" name="blog_article" rows="20" cols="100"></textarea>
Now you can get form data in view with:
if request.method == "POST":
article = addForm(request.POST)
if article.is_valid():
add_article = article.save()
print ("success!")
else:
print ("request data: ", request.POST)
print ("form is not valid")
But again, it is better and easy to render forms with built-in tags, unless you do not have a solid reason.
Source:stackexchange.com