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

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

问题描述

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

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.

如何将初始数据填充到 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()

所以如果我有

view.py

game_list = Game.objects.all()
return render_to_response('template',locals())

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

so in template.html, I could just:

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

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

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" />

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

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)

我不知道如何使用 super 进行填充.表单在运行时获取哪些数据以及如何获取?我可以阅读任何好的链接来让我开始并运行 Django Forms?

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?

这是吗

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

相当于这个?

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

模板中的变量会被替换吗?

Are the variables going to be replaced in template?

推荐答案

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

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:

尽管示例显示了从 Python 解释器进行的渲染,但在模板中执行时它是相同的.

Although the examples show the rendering working from a python interpreter, it's the same thing when performed in a template.

例如,您的模板将简单地包含:{{ f }} 而不是 print f,假设您将表单作为 f.类似地,f.as_p() 在模板中写为 {{ f.as_p }}.这在 变量 部分下的 django 模板文档中进行了描述.

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.

更新(回复评论)

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

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 }}

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

Or if you wanted to customize the HTML yourself:

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

等等.

我建议尝试一下并尝试一下.如果遇到问题,请提出问题.

I recommend trying it and experimenting a little. Then ask a question if you encounter a problem.

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

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