Creating Form automatically with ModelForm in Django 1.3


Building form in Django 1.3 is pretty easy with ModelForm. Basically, when we create Model, it will need some CRUD from then. Django give Modelform to ease our steps from defining forms which may have same description with models. For instance, I have models.py which contain Query models :

1
2
3
4
5
6
7
8
9
10
11
12
from django.db import models
from django.forms import ModelForm

class Query(models.Model):
    """Models for logging incoming query and requester"""    
    name = models.CharField(max_length=255)
    url  = models.TextField()
    source = models.CharField(max_length=255)
    request = models.CharField(max_length=255)
   
    def __unicode__(self):
        return u’%s %s %s’ % (self.name, self.source, self.request)

Then I want to show Query forms in template (index.html) which have same definition with models. So, I will use Modelform.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from django.db import models
from django.forms import ModelForm

class Query(models.Model):
    """Models for logging incoming query and requester"""    
    name = models.CharField(max_length=255)
    url  = models.TextField()
    source = models.CharField(max_length=255)
    request = models.CharField(max_length=255)
   
    def __unicode__(self):
        return u’%s %s %s’ % (self.name, self.source, self.request)

class QueryForm(ModelForm):
    class Meta:
        model = Query

On views.py, I will load this QueryModalForm :

1
2
3
4
5
6
7
8
9
10
11
12
13
from posts.models import QueryForm

def index(request, **kwargs):
    """
    Index page of Post apps
    Template: “posts/index.html“
    Context:
        object_list
            List of yahoo answers
    """
    form = QueryForm()
   
    return render_to_response(‘posts/index.html’, {‘form’: form})

On my index.html :

1
2
3
4
5
6
{% extends "base.html" %}
{% block title %} Posts Apps| Django Crawl {% endblock %}

{% block content %}
  {{ form }}
{% endblock %}

Now, It show form results that same with models.

We can use include / exclude fields on ModelForm by editing models.py :

1
2
3
4
5
….
class QueryForm(ModelForm):
    class Meta:
        model = Query
        fields = (‘name’, ‘url’, ‘source’,)

Also, we can pass data into Modalform before show in template by (edit views.py) :

On views.py, I will load this QueryModalForm :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from posts.models import QueryForm

def index(request, **kwargs):
    """
    Index page of Post apps
    Template: “posts/index.html“
    Context:
        object_list
            List of yahoo answers
    """
    data = {‘url’: ‘Put URL here …’}
    form = QueryForm(data)
   
    return render_to_response(‘posts/index.html’, {‘form’: form})

It will cut our time a lot rather re-declare again in forms.py 🙂

Reference :
https://docs.djangoproject.com/en/1.3/topics/forms/


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.