How can I set values for each HiddenInput fields of Django?
<p>I wrote codes, but I don't know how to set <code>'name'</code> and <code>'value'</code> of hidden tag with Django template. I read <a href="https://docs.djangoproject.com/ja/1.9/ref/forms/widgets/" rel="nofollow">Django's Widgets Docs</a>, but I couldn't find the way.</p>

<pre><code>(Pdb) print(errors)
&lt;ul class="errorlist"&gt;&lt;li&gt;friend_id&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;add_remove&lt;ul class="errorlist"&gt;&lt;li&gt;This field is required.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
</code></pre>

<p>First, I tried to write like</p>

<pre><code>&lt;input type="hidden" name="friend_id" value="{{ user_info.user_id }}"&gt;
</code></pre>

<p>and</p>

<pre><code>friend_id = request.POST.friend_id
</code></pre>

<p>But I couldn't get how to get POST values without Django's Form. So, I used Django's Form with following codes.</p>

<p>views.py</p>

<pre><code>from myapp.forms import HiddenUserPage
hiddenform = HiddenUserPage
if request.method == 'POST':
    hidden = hiddenform(request.POST)

if hidden.is_valid():
    from myapp.models import Friends
    try:
        friend_id = hidden.cleaned_data['friend_id']
        add_remove = hidden.cleaned_data['add_remove']
        if add_remove == "add":
            f = Friends(user_id=request.user.user_id, friend_id=friend_id)
            f.save()
        elif add_remove == "remove":
            f = Friends.objects.filter(user_id=request.user.user_id).get(friend_id=friend_id)
            f.delete()
    except:
        errors = "DB error"
    else:
        errors = hidden.errors
else:
    hidden = hiddenform()
    errors = ""


view = { 
    'errors': errors,
    'hidden': hidden,
}

template = 'myapp/user/user_page.html'
return render(request, template, view)
</code></pre>

<p>forms.py</p>

<pre><code>class HiddenUserPage(forms.Form):

    friend_id = forms.CharField(widget=forms.HiddenInput())
    add_remove = forms.CharField(widget=forms.HiddenInput())
</code></pre>

<p>user_page.html</p>

<pre><code>    &lt;form method="POST" action="" class=""&gt;
        {% csrf_token %}
        &lt;p class="submit"&gt;
            &lt;button class="confirmbutton" type="submit"&gt;
                {% if is_friend %}
                    remove friend
                &lt;!-- # I'd like to write like # --&gt;
                &lt;!-- &lt;input type="hidden" name="friend_id" value="remove"&gt; # --&gt;
                &lt;!-- &lt;input type="hidden" name="friend_id" value="{{ user_info.user_id }}"&gt; # --&gt;
                    {{ hidden.add_remove }}
                    {{ hidden.friend_id }}
                {% else %}
                    add friend
                &lt;!-- &lt;input type="hidden" name="friend_id" value="add"&gt; # --&gt;
                &lt;!-- &lt;input type="hidden" name="friend_id" value="{{ user_info.user_id }}"&gt; # --&gt;
                    {{ hidden.add_remove }}
                    {{ hidden.friend_id }}
                {% endif %}
            &lt;/button&gt;
        &lt;/p&gt;
    &lt;/form&gt;
</code></pre>

<p>Sorry, my code is filthy.</p>