0

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.

2
  • Please post your actual code for example if your add method recieves a GET it's going to throw an exception. Commented May 16, 2016 at 23:59
  • See my answer below. It is probably because of wrong name of blog_article field in html form. Commented May 17, 2016 at 0:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try to work with the {{ form.as_p }} type stuff if that's really the best way. Thanks for the advice!
It is easiest way. Even you need to add some custom css to form elements in the future, you can add your class names while creating them in forms.py. You can research it. There are tones of futures in built-in forms! @BenHanson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.