gpt4 book ai didi

django crispy 表单文件上传

转载 作者:行者123 更新时间:2023-12-01 07:55:03 26 4
gpt4 key购买 nike

我在使用 django crispy 表单正确上传图像字段时遇到问题。表单和页面上的其他所有内容都可以正常工作,但图像字段不会出错或保存,它只是直接越过它。我可以很好地通过管理员添加图像,它只在我的 crispy 表单上失败。

#views.py
@login_required
def dashboard(request, **kwargs):
'''
perform managerial and administrative tasks for a blog. Only
available to the blog owner
'''
alert_message = ""
status = ""

blog_slug = kwargs['blog']
blog = get_object_or_404(PersonalBlog, slug=blog_slug)

# handle the form submit to update blog data
form = BlogEditForm(instance=blog)
if request.method == "POST":
if blog.owner == request.user:
form = BlogEditForm(request.POST, instance=blog)

if form.is_valid():
form.save()

alert_message = "Your blog data has been updated successfully"
status = "saved"
else:
alert_message = "There was a problem saving the data you entered. Please correct the errors above."
status = "error"
else:
alert_message = "You do not have access to update this blog's information."
status = "error"


return render(request, "blogcontent/dashboard.html", {'alert_message':alert_message,
'status':status, 'form':form})


#forms.py
class BlogEditForm(ModelForm):
description = forms.CharField(widget = forms.Textarea())
twitter = forms.CharField(required=False, help_text="show twitter feed, and allow people to interact with you on twitter")
twitter_widget_id = forms.CharField(required=False, help_text="required to show a timeline widget for your twitter account. " + \
"<span class='glyphicon glyphicon-question-sign'></span>")
instagram = forms.CharField(required=False, help_text="show instagram feed on your blog page")
disqus = forms.CharField(required=False, help_text="allow comments at the bottom of your blog posts")

class Meta:
model = PersonalBlog

def __init__(self, *args, **kwargs):
super(BlogEditForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
'<h2 class="text-center">Edit data about your blog</h2>',
Field('owner', type="hidden"),
Field('title', type="hidden"),
Div('description', css_class="col-md-12"),
Div('twitter', css_class="col-md-6"),
Div('twitter_widget_id', css_class="col-md-6"),
Div('instagram', css_class="col-md-6"),
Div('disqus', css_class="col-md-6"),
Div('logo', css_class="col-md-12"),
),
ButtonHolder(
Submit('submit', 'Update Info', css_class='btn-lg'),
css_class="text-center"
),

)

#dashboard.html
<form method="post" enctype="multipart/form-data">
{% crispy form %}
</form>

最佳答案

啊,我就知道这会很蠢。我没有将 request.FILES 附加到 form.save() 对象。

form = BlogEditForm(request.POST, request.FILES, instance=blog)

关于django crispy 表单文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25632918/

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