1

I have a form that has a field:

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

Then in a template I want to put a user's nickname value. I am using django-widget-tweaks.

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

In this case I get an empty string instead of the value.

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

And in this case I get a string "request.user" instead of the value of request.user.

How can I add a value from my database to this field?

tar
  • 1,538
  • 1
  • 20
  • 33
Tapo4ek
  • 131
  • 1
  • 6

2 Answers2

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

Community
  • 1
  • 1
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
0
{{ form.nickname|attr:"value:request.user" }
catherine
  • 22,492
  • 12
  • 61
  • 85