【发布时间】:2016-02-25 15:20:43
【问题描述】:
是否可以将非表单元素注入到动态 Django 表单的上下文中?我有一个“删除用户照片”表单,我想在其中包含每个用户的缩略图,并在其下方添加一个 BooleanField 复选框和标签:
+------------+
| |
| photo |
| |
+------------+
[x] Delete <username>'s photos
现在我知道如何创建动态复选框及其标签,但我不确定如何添加每个用户的照片。从我下面的代码中可以看出,每个 HTML 输入标签的 name 属性将包含用户的 ID,我将在用户提交表单时检查此属性以确定是否删除他们的照片。我想在链接到用户个人资料照片的每个输入标签上方插入一个标签。图像标签的“src”属性将包含用户 ID,该 ID 创建指向其照片的链接。有没有办法将这个非表单图像标签“注入”到这个动态表单的上下文中,以便在每个复选框输入标签上方呈现一个图像标签?
谢谢。
# views.py
def remove_access_to_private_photos(request, template):
if request.method == 'POST':
form = RemovePrivatePhotoAccessForm(request.POST, this_user_id=request.user.id)
if form.is_valid():
for name, value in form.cleaned_data.items():
if value == True:
# Profile links to User via a OneToOneField
this_user = Profile.objects.get(user_id=request.user.id)
other_user = Profile.objects.get(user_id=name)
this_user.remove_private_access(other_user_prof)
return redirect('photos-home')
else:
form = RemovePrivatePhotoAccessForm(this_user_id=request.user.id)
context = {'form': form}
return render(request, template, context)
# models.py
class RemovePrivatePhotoAccessForm(forms.Form):
def __init__(self, *args, **kwargs):
this_user_id = kwargs.pop('this_user_id')
super(RemovePrivatePhotoAccessForm, self).__init__(*args, **kwargs)
user = User.objects.get(pk=this_user_id)
user_prof = Profile.objects.get(user=user)
other_user_id_list = user_prof.gave_private_access().values_list('user_id', flat=True)
for id in other_user_id_list:
other_user = User.objects.get(pk=id)
self.fields[str(id)] = forms.BooleanField(required=False)
self.fields[str(id)].label = mark_safe('<a href="/photos/delete/%s/%s">%s</a>') % (id, this_user_id, other_user.username)
# delete_photos.html
<form action="." method="post">
{% csrf_token %}
{% for field in form %}
{# I'D LIKE TO PUT <IMG> TAG HERE #}
{{ field }} Delete {{ field.label|safe }}'s photos
{% endfor %}
<input type="submit" value="Submit" />
</form>
【问题讨论】:
-
表单只是一个类。它可以具有您想通过 args 或 kwargs 分配的任何属性。
-
对,但我不明白在图像不属于表单上下文的情况下如何渲染图像。我的模板是说,“for field in form...”如果图像不是“form.field”或“form.label”,你将如何将它注入到动态表单中?
-
我给你加个例子。
标签: django django-models django-forms django-templates