yamachan
yamachan

Reputation: 1079

How can I set values for each HiddenInput fields of Django?

I wrote codes, but I don't know how to set 'name' and 'value' of hidden tag with Django template. I read Django's Widgets Docs, but I couldn't find the way.

(Pdb) print(errors)
<ul class="errorlist"><li>friend_id<ul class="errorlist"><li>This field is required.</li></ul></li><li>add_remove<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

First, I tried to write like

<input type="hidden" name="friend_id" value="{{ user_info.user_id }}">

and

friend_id = request.POST.friend_id

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

views.py

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)

forms.py

class HiddenUserPage(forms.Form):

    friend_id = forms.CharField(widget=forms.HiddenInput())
    add_remove = forms.CharField(widget=forms.HiddenInput())

user_page.html

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

Sorry, my code is filthy.

Upvotes: 2

Views: 3462

Answers (1)

Nikita
Nikita

Reputation: 6341

Looks like the question is in providing initial data to the form, then it's is generally done in the view passing initial to the form instantiation, e.g.:

# In your view.py
def ...(...):
    # Inside your view function 
    if request.method == 'GET':
        # Provide initial data to the form here
        # Get your 'user_info' from models or sessions,
        # or wherever you keep it
        hidden = hiddenform(initial={"friend_id":user_info.user_id})
    if reuest.method == 'POST':
        hidden = hiddenform(request.POST)
        # Process posted form data
        ...
    # More code general for both HTTP verbs
    view = {'errors': errors, 'hidden': hidden}
    template = 'myapp/user/user_page.html'
    return render(request, template, view)

You might also want to bound the form to model data directly, see the docs for more info.

Upvotes: 2

Related Questions