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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…