gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:16:13 25 4
gpt4 key购买 nike

我写了代码,但我不知道如何用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 的表单和以下代码。

View .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)

表单.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 传递给表单实例化的 View 中完成,例如:

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

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

关于python - 如何为 Django 的每个 HiddenInput 字段设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com