views:

51

answers:

3

I decide to learn Django Forms. For awhile now been using HTML forms because its hard for me to come 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 wanna populate an initial data on html forms which i usually use:

    {% 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? By reading article online,they do this by overriding using forms.init

class Anyforms(forms.Form):
   super(Anyforms, self).__init__(*args,**kwargs)

I cant get a hold of how to populate using super. What/How data that forms get during runtime? Any good links that i could read to get me up & running on wrangling django forms?

Does:

<input="text" name="title" value="{{game.title}}" /> 
<input="text" name="genre" value="{{game.genre}}" /> 

are the same as this ?

data = {'title':'{{game.title}}','genre':'{{game.genre}}'} 
form(data) 

Are the variable gonna be replace in template?

+1  A: 

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)
S.Lott
if the data isn't valid the user will be shown errors :(, but if you pass the data as the `initial` kwarg it will populate the fields with no errors `f = ContactForm(initial=data)`
Jiaaro
+2  A: 

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.

ars
data = {'id':'{{ game.id }}','position':'{{game.id}}'}form = UserQueueForm(initial=data)Are these gonna work?
diehell
<input="text" name="title" value="{{game.title}}" /><input="text" name="genre" value="{{game.genre}}" />are the same as thisdata = {'title':'{{game.title}}','genre':'{{game.genre}}'}form(data)Is the variable gonna be replace in template?
diehell
@user459885: I updated the question to respond to your comments.
ars
Thanks ars. So i had to initialize the value at view. In your example, it is for a single instance where Game.objects.get(id=1). What if im using Game.objects.all(). In the template im iterating on it using {% for game in Game.objects.all() %} then {{game.title}} {{game.position}}if i were to use html form, i could just render every iterable objects by <input="text" name="title" value="{{game.title}}" /> <input="text" name="genre" value="{{game.position}}" />How could i achieve the same result using django forms?Thank you for entertaining my dumb question. Appreciate it a lot.
diehell
@diehell: You want to use formsets. I suggest you start with reading the docs on model formsets: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#model-formsets -- if you encounter any problems, start a new question rather than continue in the comments here, to make it easier and keep things distinct.
ars
A: 

just change

data = {'title':'{{game.title}}','genre':'{{game.genre}}'} 
form(data) 

to

data = {'title':'{{game.title}}','genre':'{{game.genre}}'} 
form(initial=data) 
Jiaaro