初始填充在Django表单上 [英] Initial populating on Django Forms

查看:100
本文介绍了初始填充在Django表单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定学习Django表单。一段时间以来,我一直在使用HTML表单,因为我很难用Django表单来表达。



我如何将初始数据填充到Django Forms?
示例:



考虑这些模型是否已填充。包含数据。



models.py

  class Game(models.Model):
title = models.CharField()
genre = models.CharField()

所以如果我有



view.py

  game_list = Game.objects.all()
return render_to_response('template',localals())
/ pre>

所以在 template.html 中,我可以:

  {%for game in game_list%} 
< p> game.title< p> < br /> < p为H. game.genre< p>

如果我想在使用HTML表单时填充初始数据,这是我通常会做的:

  {%game_list%}中的游戏
< form action ='/ add / 'method ='POST'>
< input =textname =titlevalue ={{game.title}}/>
< input =textname =genrevalue ={{game.genre}}/>
< input type =submit/>

如何在Django Forms中执行此操作?
从我在网上阅读文章看,他们通过覆盖使用表单来做到这一点.__ init __



$ _ code class anyforms(forms.Form):
super(Anyforms,self).__ init __(* args,** kwargs)

我无法掌握如何使用 super 填充。运行时获取的数据是什么?
我可以阅读的任何良好的链接,让我在Django表单上争吵并运行?



这是

 < input =textname =titlevalue ={{game.title}}/> 
< input =textname =genrevalue ={{game.genre}}/>

相当于此?

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

变量是否要在模板中替换?

解决方案

S。 Lott的答案告诉您如何在视图中使用某些数据初始化表单。要在模板中呈现您的表单,请参阅django文档的以下部分,其中包含许多示例:





虽然示例显示了从python解释器工作的渲染,但是在模板中执行时也是一样的。



例如,您的模板将不包含 print f ,而只包含: {{ f}} 假设您通过上下文将您的表单作为 f 。同样, f.as_p()以模板的形式写为 {{f.as_p}} 。这在变量部分的django模板文档中有描述。



更新(回复评论)



不完全是,模板符号仅适用于模板。您的表单和相关数据将在视图中初始化。



所以,使用您的示例,您的视图将包含以下内容:

  def view(request):
game = Game.objects.get(id = 1)#只是一个例子
data = {'id':game.id,'position':game.position
form = UserQueueForm(initial = data)
return render_to_response('my_template.html',{'form':form})

然后你的模板会有如下的东西:

  {{form}} 

或者如果您想自己自定义HTML:

  {{form.title}}< br /> 
{{form.genre}}< br />

等等。



我建议尝试一下,然后尝试一下。如果遇到问题,请问一个问题。


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?

解决方案

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.

这篇关于初始填充在Django表单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆