如何为 Django 的每个 HiddenInput 字段设置值?

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

我写了代码,但是我不知道如何用Django模板设置隐藏标签的'name''value'。我看了Django's Widgets Docs,但找不到路

(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>

首先,我试着写成

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

friend_id = request.POST.friend_id

但是如果没有 Django 的 Form,我不知道如何获取 POST 值。所以,我使用了 Django 的表单和以下代码。

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>

抱歉,我的代码很脏。

看起来问题在于向表单提供初始数据,然后通常在将 initial 传递给表单实例化的视图中完成,例如:

# 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)

您可能还想直接将表单绑定到模型数据,请参阅 the docs for more info