Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Forms: A Beginners Guide

Tags:

django-forms

I have been struggling to create a django form which users can fill and the filled data gets saved in the database. I used the docs available for django modelforms but was unsuccessfull in building a working form. I even googled for howto's about building a form that saves data to the database, there are hardly any available. When I tried to learn I ended up asking questions here (on this site). But however many questions I ask, I should be specific about my issue in the questions I ask, which means if one issue is solved (from the answers here) another issue is going to crop up. So my request is it would be a great help if some one could come up with a basic form using formsets with a relevant model (with a charfield, choicefield, radiofield, textarea) view, template (form and redirect), urls. And also a way to view the user-entered data in the admin page. At least whovever wants to build a django-form he/she can have something to begin with, which can be modified or improved upon without much hassel.

like image 575
nixnotwin Avatar asked Feb 20 '11 13:02

nixnotwin


People also ask

How do I create a form in Django?

To create a Form , we import the forms library, derive from the Form class, and declare the form's fields. A very basic form class for our library book renewal form is shown below — add this to your new file: from django import forms class RenewBookForm(forms. Form): renewal_date = forms.

Is Django a beginner?

Django for Beginners is a project-based introduction to Django, the popular Python-based web framework. Suitable for total beginners who have never built a website before as well as professional programmers looking for a fast-paced guide to modern web development and Django fundamentals.

Is Django forms necessary?

It's recommended to use the forms api provided by Django since it allows you to reuse, validate, and customize your forms. As to whether or not it is a good practice that depends completely on you but if you are trying to scale an application I would recommend use the forms.

How do forms work in Django?

Working with form templates. All you need to do to get your form into a template is to place the form instance into the template context. So if your form is called form in the context, {{ form }} will render its <label> and <input> elements appropriately.


1 Answers

I started writing up a long post about this based on a site I created... but the issue is that I was just (poorly) reproducing this: http://docs.djangoproject.com/en/1.2/intro/tutorial01/

My advice to you is to go through this tutorial. I personally found it extremely helpful when I was picking up Django. In part four there's a great example of submitting user data from a template, dealing with it in a view, and saving it using a model.

Edit: based on your comment below I see you're using modelform. I've used this once before, but found it a bit limited when it came to styling. When I did, I followed this walkthrough: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Note that you still have to include the form data in your template, as well as generate the form in your view, although the modelform does take some of the work out of it for you. Here's another walkthrough you may find useful: http://themorgue.org/blog/2008/05/14/django-and-modelform/

like image 138
Gavin Anderegg Avatar answered Sep 28 '22 08:09

Gavin Anderegg