Creating forms from models

Go To StackoverFlow.com

0

i'm moving my first steps into Django and i'm tryin' to follow the tutorial at this link: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/ but i can't get a form. It just returns a white page!

Can someone explain if i'm missing something?

my models.py:

from django.db import models
from django.forms import ModelForm

class UserProfile(models.Model):
    name = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    email = models.EmailField()
    tel = models.CharField(max_length=30, null=True, blank=True)
    def __unicode__(self):
        return '%s %s' % (self.surname, self.name)

class Ad(models.Model):
    title = models.CharField(max_length=50)
    descr = models.TextField()
    city = models.CharField(max_length=30)
    price = models.FloatField(null=True, blank=True)
    img = models.ImageField(upload_to='./uploaded_imgs', null=True, blank=True)
    dateIn = models.DateField(auto_now_add=True)
    dateExp = models.DateField(auto_now_add=True)
    codC = models.ForeignKey('Cat')
    codU = models.ForeignKey('UserProfile')
    def __unicode__(self):
        return '%s - %s' % (self.title, self.dateIn)

class Cat(models.Model):
    cat = models.CharField(max_length=35, primary_key=True)
    def __unicode__(self):
        return self.cat

my forms.py:

from django import forms

class newAdForm(forms.Form):
    title = forms.CharField(max_length=50)
    descr = forms.CharField(widget=forms.Textarea)
    city = forms.CharField(max_length=30)
    price = forms.FloatField(required=False)
    img = forms.ImageField(required=False)
    dateIn = forms.DateField(auto_now_add=True)
    dateExp = forms.DateField(auto_now_add=True)
    codC = forms.ModelChoiceField(Cat)
    codU = forms.ModelChoiceField(UserProfile)

my views.py:

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django.http import HttpResponse
from django import forms
from models import *
from django.forms import *
from django.forms.models import modelformset_factory

[...]

def newAd(request):
    newAdFormSet = modelformset_factory(Ad)
    if request.method == 'POST':
        formset = newAdFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            return render_to_response('conf.html', {'state':'Your ad has been successfull created.'}, context_instance = RequestContext(request),)
    else:
        formset = newAdFormSet()
    return render_to_response('ad_form.html', context_instance = RequestContext(request),)

my ad_form_auto.html template:

{% extends "base.html" %}
{% block title %}Ads insertion form{% endblock %}
{% block content %}
    {% if error_message %}
        <p><strong>{{ error_message }}</strong></p>
    {% endif %}
<form method="post" action="">
    {{ formset }}
</form>
{% endblock %}

Thanks a lot! This community looks just awesome! :)

2012-04-03 21:38
by Mark


2

You're not passing the form to the template - at present your 'formset' variable containing the form isn't included in the datadict being passed to the view.

return render_to_response('ad_form.html', context_instance = RequestContext(request),)

Should include the data (see render_to_response() docs) here I've renamed the form in your data and to your template as 'form' for ease of nomenclature:

return render_to_response('ad_form.html',
                          {'form':formset},
                          context_instance=RequestContext(request))

Then, in your template (see forms in templates docs) replace {{formset}} with {{form.as_p}}. Also add a submit button to the form.

<form action="" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
2012-04-03 21:46
by jvc26
i was experiencing problems with csrf on the login function so i've disabled it for the moment. with the forms it works great instead! so, i have the form working now, but instead of a unique white form i receive 2 forms: one filled with an old tuple and one white. i supposed to receive only the blank one correct? is there any other error - Mark 2012-04-05 23:18