1

我有一个包含字段的表单:

class ProfileEditForm(forms.Form):
    nickname = forms.CharField(max_length=30, label=_('Your nickname'), required=False)

然后在模板中我想放置用户的昵称值。我正在使用django-widget-tweaks.

{% load widget_tweaks %}
<label>Your nickname</label> {{ form.nickname|append_attr:"value:"|add:"request.user" }}

在这种情况下,我得到一个空字符串而不是值。

<label>Your nickname</label> {{ form.nickname|append_attr:"value:request.user" }}

在这种情况下,我得到一个字符串“request.user”而不是request.user.

如何将数据库中的值添加到此字段?

4

2 回答 2

0

You're looking for the initial argument, which you can pass in the value you want to use to prepopulate the element. You can read about it here in the docs, or from a related SO question here: https://stackoverflow.com/a/604325/37386

于 2013-03-31T23:40:21.157 回答
0
{{ form.nickname|attr:"value:request.user" }
于 2013-04-01T02:12:30.103 回答