Python Django表单字段未显示
因此,我尝试在图像页面上设置一个复选框,如果我想将此图像视为“配置文件图像”,则可以设置布尔字段true/false。问题是表单字段选项没有显示在模板中。有什么建议吗 single_image.htmlPython Django表单字段未显示,python,html,django,forms,Python,Html,Django,Forms,因此,我尝试在图像页面上设置一个复选框,如果我想将此图像视为“配置文件图像”,则可以设置布尔字段true/false。问题是表单字段选项没有显示在模板中。有什么建议吗 single_image.html <form method='POST' action="{% url 'set_profile_image' plant_pk=plnt.pk image_pk=img.pk %}"> {% csrf_token %} <hr>
<form method='POST' action="{% url 'set_profile_image' plant_pk=plnt.pk image_pk=img.pk %}">
{% csrf_token %}
<hr>
{{ form.as_p }}
<hr>
<input type="submit" value="Submit">
views.py
class SingleImageView(DetailView):
""" A view to see a single image."""
template_name = "project/single_image.html"
queryset = Image.objects.all()
def get_context_data(self, **kwargs):
"""Return a dictionary with context data for this template to use."""
# get the default context data:
# this will include the Profile record for this page view
context = super(SingleImageView, self).get_context_data(**kwargs)
img = Image.objects.get(pk=self.kwargs['image_pk'])
plnt = Image.objects.get(pk=self.kwargs['image_pk']).plant
context['img'] = img
context['plnt'] = plnt
form = SetProfileImageForm()
context['set_profile_image'] = form
# return the context dictionary
return context
def get_object(self):
"""Returns the Note Object that should be deleted."""
# read the URL data values into variables
plant_pk = self.kwargs['plant_pk']
image_pk = self.kwargs['image_pk']
# find the StatusMessage object, and return it
return Image.objects.get(pk=image_pk)
def set_profile_image(request, plant_pk, image_pk):
"""A custom view function to set profile image."""
# find the plant for whom we are setting the image
plant = Plant.objects.get(pk=plant_pk)
if request.method == 'POST':
if "cancel" in request.POST:
return redirect('display_plant', pk=plant.pk)
form = SetProfileImageForm(request.POST or None, request.FILES or None)
if form.is_valid():
plant.set_profile_image(image_pk)
return redirect('display_plant', pk=plant.pk)
else:
print("Error: the form was not valid.")
else:
return reverse('gallery', kwargs={'pk':plant.pk})
您正在将
表单
实例作为上下文中的set\u profile\u img
键发送。请在HTML中更改它,或只是重命名上下文键
。。。
上下文['form']=form
{%csrf_令牌%}
{{form.as_p}}
或
。。。
上下文['set_profile_image']=form
{%csrf_令牌%}
{{set_profile_image.as_p}
在哪里将表单传递到上下文?@WillemVanOnsem在我的singleimageview detailview类中,我在get\u context\u数据中传递表单。编辑:编辑原始帖子以传达这一令人敬畏的信息,用于显示表单。但我似乎无法立即修改模型中的布尔字段:我刚刚意识到我省略了一个保存函数。现在效果很好。谢谢
class SingleImageView(DetailView):
""" A view to see a single image."""
template_name = "project/single_image.html"
queryset = Image.objects.all()
def get_context_data(self, **kwargs):
"""Return a dictionary with context data for this template to use."""
# get the default context data:
# this will include the Profile record for this page view
context = super(SingleImageView, self).get_context_data(**kwargs)
img = Image.objects.get(pk=self.kwargs['image_pk'])
plnt = Image.objects.get(pk=self.kwargs['image_pk']).plant
context['img'] = img
context['plnt'] = plnt
form = SetProfileImageForm()
context['set_profile_image'] = form
# return the context dictionary
return context
def get_object(self):
"""Returns the Note Object that should be deleted."""
# read the URL data values into variables
plant_pk = self.kwargs['plant_pk']
image_pk = self.kwargs['image_pk']
# find the StatusMessage object, and return it
return Image.objects.get(pk=image_pk)
def set_profile_image(request, plant_pk, image_pk):
"""A custom view function to set profile image."""
# find the plant for whom we are setting the image
plant = Plant.objects.get(pk=plant_pk)
if request.method == 'POST':
if "cancel" in request.POST:
return redirect('display_plant', pk=plant.pk)
form = SetProfileImageForm(request.POST or None, request.FILES or None)
if form.is_valid():
plant.set_profile_image(image_pk)
return redirect('display_plant', pk=plant.pk)
else:
print("Error: the form was not valid.")
else:
return reverse('gallery', kwargs={'pk':plant.pk})