0

After I defined MyForm in the forms.py with a HiddenInput with dummy value, I want to render as forms as users I've in my database and with username as hidden input.

Template Django:

{% for username,time in llistat_usuaris.items %}

{{ form.as_p }} # HOW CAN I SET {{USERNAME}} as hidden value ??

{% endfor %}    
  • Why not just give form initial values when initializing the form? – Odif Yltsaeb May 23 '14 at 09:01
  • You should not do that in the template but rather use the Forms to do that. In your case maybe even a FormSet is useful. – toabi May 23 '14 at 09:06
  • @OdifYltsaeb Is a bad idea I think because I don't know how many users I have and how many forms I have to render in my view – user3584455 May 23 '14 at 09:11
  • @toabi Could you give me an example? – user3584455 May 23 '14 at 09:11
  • @user3584455 Well, see https://docs.djangoproject.com/en/1.6/topics/forms/formsets/ - As I don't know what exactly you want to achieve and how your data is structured, it's hard to tell what's the best. – toabi May 23 '14 at 11:36

1 Answers1

0

Are you looking for this ?

{{ form.field.as_hidden }}

".as_hidden" this makes any form field to be invisible to the users, but you receive the field in the post as others

I usually create a field like a normal field, and in the template I add "as_hidden", so django won't display this field in the browser.

I suggest you to check this posts:

Hidden Field in Django model

Django forms hidden model field

Django ModelForm to have a hidden input

But if you want to get the username of the user who makes the post, is easier than that, you only need to access the request.user

Community
  • 1
  • 1
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • I want a way to call a view with a dynamic argument, the argument is the selected username – user3584455 May 23 '14 at 10:28
  • What is exactly what you're trying to do ? You can pass arguments from different ways: post, get, request, session, middleware... My problem is I don't understand what is exactly what you're trying to do – AlvaroAV May 23 '14 at 10:57
  • What you're trying to do is to change the value of hidden input username in the template? – AlvaroAV May 23 '14 at 10:59