2

Django 1.8 / Python 3.4

I wanna add data from an html-form via a Django view named "add" to my database. However, for some reason this just doesn't happen and I can't figure out what's wrong. Presumably the mistake is in the view's code, but what exactly do I need to change?

models.py

from django.db import models
from model_utils import Choices

class Entry(models.Model):
    user = models.CharField(max_length=50)
    title = models.CharField(max_length=200)
    description = models.TextField()
    due_date = models.DateField('Date')
    due_time = models.TimeField('Time')
    STATUS = Choices('Open', 'Done')
    status = models.CharField(choices=STATUS, default=STATUS.Open, max_length=4)

def __unicode__(self):
    return u"%s %s %s %s %s" % (self.user, self.title, self.description, self.expiry, self.status)

def expiry(self):
    return u"%s %s" % (self.due_date, self.due_time)

The interesting part of my add.html

    <td><input type="text" name="title"></td>
    <td><input type="text" name="description"></td>
    <td><input type="text" name="due_date"></td>
    <td><input type="text" name="due_time"></td>
    <td>
        <select name="status" size="1" selected value="Open">
            <option>Open</option>
            <option>Done</option>
        </select>
    </td>

forms.py

from django import forms
from django.forms.widgets import TextInput

class EntryForm(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.widgets.TextInput()
    due_date = forms.DateField()
    due_time = forms.TimeField()
    status = forms.ChoiceField(choices=[(x, x) for x in range(1, 2)])

And the relevant view in my views.py

from django import forms
from django.shortcuts import render
from django.shortcuts import redirect
from website.list.forms import EntryForm

def add(request):
    if request.method == "POST":
        form = EntryForm(request.POST) 
        if form.is_valid():
            new_entry = form.save()
            new_entry.save()
            return redirect('website')
    else:
        form = EntryForm() 
    return render(request,'add.html', {'form': form})

Any help is appreciated!


[EDIT]

So, this is what my add.html looks like now:

<form action="." method="post">
{% csrf_token %}
{{ form }}
    <br><input type="submit" value="Send"/>
    <br><br><a href="{% url 'overview' %}">Cancel</a>
</form>

And the slightly edited views.py again:

from django import forms
from django.shortcuts import render
from django.shortcuts import redirect
from www.todolist.forms import EntryForm
from django.contrib.auth.models import User

def add(request):
    if request.method == "POST":
        form = EntryForm(request.POST) 
        if form.is_valid())
            form.save()
            return redirect('website')
    else:
        form = EntryForm() 
    return render(request,'add.html', {'form': form})

2 Answers 2

2

Figured it out ... This is what forms.py has to look like in order for the save() function to work in the view:

class EntryForm(forms.ModelForm):
CHOICES = (
    ('1', 'Open'),
    ('2', 'Done'),
)
title = forms.CharField(max_length=200)
description = forms.CharField(widget=forms.Textarea)
due_date = forms.DateField()
due_time = forms.TimeField()
status = forms.ChoiceField(choices=CHOICES)

class Meta:
    model = Entry
    fields = '__all__'

The important things to notice are "ModelForm" instead of just "Form" and the class Meta information.

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

Comments

0

I am guessing you are getting an error here; form = EntryForm(request.POST) but because you are manually writing out the form html instead of using the Django form to do it, you aren't seeing the error.

https://docs.djangoproject.com/en/1.8/topics/forms/#the-template is how you should use Django to render your html for your Django form; and this by default will display any errors the happened when trying to validate your data.

4 Comments

I guess you're right because the redirect isn't happening either. So probably something goes wrong before the save() to database is reached?! I'm gonna read through the page you linked ...
Please take a look at my EDIT in the start post: This now gives me the following error message ... "AttributeError at /add/ 'EntryForm' object has no attribute 'save'"
Oh, you might want to use a forms.ModelForm instead of just forms.Form. The latter doens't have a save method associated with it. docs.djangoproject.com/en/1.8/topics/forms/modelforms/…
Yes, indeed I do. Please see my answer below.

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.