Ben Hanson
Ben Hanson

Reputation: 25

Django Forms Returns Error About Errorlist When Validating

I am new to Django and am attempting to make a simple blog. I'm currently attempting to make a form that will only appear to admins that allows them to add articles to the database. However, I'm running into a problem that article.is_valid() keeps failing. I've worked on this for a while and have narrowed it down to an issue of some sort with the errorlist, but I'm not sure what it is.

Here's the relevant code: views.py

from django.shortcuts import render
from django.http import HttpResponse, HttpRequest, Http404

from .forms import addForm

# Create your views here.
def index(request):
    return render(request, 'blogposts/index.html')

def page_add(request):
    return render(request, 'blogposts/add.html')

def add(request):
    if request.method == "POST":
        article = addForm(request.POST)
    if article.is_valid():
        add_article = article.save()
        print "success!"
    else: 
        print article['errorlist']
else:
    print "oops"
return render(request, 'blogposts/add.html')

forms.py

from django import forms
from .models import Articles
from django.forms import ModelForm

class addForm(forms.ModelForm):
    class Meta: 
        model = Articles
        fields = ['blog_author', 'blog_article', 'blog_date', 'errorlist']

*I've tried it both with and without the errorlist included in the fields on forms.py; I'm not clear on if this is necessary or not. It seems to get further down the process if I do so.

models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.

# Controls blog post storage

class Articles(models.Model):
    blog_author = models.TextField()
    blog_article = models.TextField()
    blog_date = models.DateField()
    errorlist = models.TextField()
    class Meta: 
        db_table = "Articles"

add.html (the page with the form)

<!-- Form for adding articles to the database -->
<form action = "/home/add/" method = "post">
    {% csrf_token %}
    <label for = "article">Article: </label>
    <textarea id = "article" name = "article" rows = "20" cols = "100"></textarea>
    <label for = "author"> Author: </label>
    <select name = "author">
        <option value = "Jimmy Liu">Jimmy Liu</option>
        <option value = "Ben Hanson">Ben Hanson</option>
    </select>
    <label for = "date">Date Published: </label>
    <input type = "date" name = "date"> 
    <input type = "submit" value = "submit">
</form>

When I run this, I get the:

else: 
    print article['errorlist']

message in my console. It successfully takes the data (it doesn't print Oops), but it never saves that data to my database.

Thanks in advance for any assistance.

Upvotes: 0

Views: 320

Answers (1)

alioguzhan
alioguzhan

Reputation: 7917

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.

Upvotes: 1

Related Questions