I decided to learn Django Forms. For a while now, I have been using HTML forms because it's hard for me to come to terms with Django Forms.
How could I populate initial data to Django Forms? Example:
Consider if these models are populated. Contain data.
models.py
class Game(models.Model): title = models.CharField() genre = models.CharField()
so if I have
view.py
game_list = Game.objects.all() return render_to_response('template',locals())
so in template.html, I could just:
{% for game in game_list %} <p> game.title <p> <br /> <p> game.genre <p>
If I want to populate initial data when using HTML forms, this is what I usually do:
{% for game in game_list %} <form action= '/add/' method='POST'> <input="text" name="title" value="{{game.title}}" /> <input="text" name="genre" value="{{game.genre}}" /> <input type="submit" />
How can I do this in Django Forms? From what I've seen by reading articles online, they do this by overriding using forms.__init__
:
class Anyforms(forms.Form): super(Anyforms, self).__init__(*args,**kwargs)
I can't get a hold of how to populate using super
. What data do forms get during runtime and how? Any good links that I could read to get me up and running on wrangling Django Forms?
Is this
<input="text" name="title" value="{{game.title}}" /> <input="text" name="genre" value="{{game.genre}}" />
equivalent to this?
data = {'title':'{{game.title}}','genre':'{{game.genre}}'} form(data)
Are the variables going to be replaced in template?
initial is used to change the value of the field in the input tag when rendering this Field in an unbound Form. initial accepts as input a string which is new value of field. The default initial for a Field is empty. Let's check how to use initial in a field using a project.
To populate initial values on Python Django forms, we can create a form instance with the initial argument set. in the view view function. In it, we create the UserQueueForm with the initial argument set to the data dict to set the initial value of the form fields with name id and position .
The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.
parameter [Django-doc]. This prefix parameter will add a prefix to all the form input items that arise from that form. For example if we specify prefix='father' for the FatherForm , then the name of the items will be father-name , and father-first_name .
S. Lott's answer tells you how to initialize the form with some data in your view. To render your form in a template, see the following section of the django docs which contain a number of examples:
Although the examples show the rendering working from a python interpreter, it's the same thing when performed in a template.
For example, instead of print f
, your template would simply contain: {{ f }}
assuming you pass your form through the context as f
. Similarly, f.as_p()
is written in the template as {{ f.as_p }}
. This is described in the django template docs under the Variables section.
Update (responding to the comments)
Not exactly, the template notation is only for template. Your form and associated data are initialized in the view.
So, using your example, your view would contain something like:
def view(request): game = Game.objects.get(id=1) # just an example data = {'id': game.id, 'position': game.position} form = UserQueueForm(initial=data) return render_to_response('my_template.html', {'form': form})
Then your template would have something like:
{{ form }}
Or if you wanted to customize the HTML yourself:
{{ form.title }} <br /> {{ form.genre }} <br />
and so on.
I recommend trying it and experimenting a little. Then ask a question if you encounter a problem.
http://docs.djangoproject.com/en/1.2/ref/forms/api/#ref-forms-api-bound-unbound
To bind data to a form, pass the data as a dictionary as the first parameter to your Form class constructor:
>>> data = {'subject': 'hello', ... 'message': 'Hi there', ... 'sender': '[email protected]', ... 'cc_myself': True} >>> f = ContactForm(data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With