Django使用带有ImageField和User的表单(Django using a form with an ImageField and a User)

编程入门 行业动态 更新时间:2024-10-26 23:40:23
Django使用带有ImageField和User的表单(Django using a form with an ImageField and a User)

在Django中,用户可以上传带有图像的评论。

from sorl.thumbnail import ImageField class Comment(models.Model): count_votes = models.Integer(default=0) user = models.ForeignKey(User) thumb = ImageField(upload_to="thumbnails") # ...

这就是我想要做的:

# views.py def add_comment(request): if request.method == 'POST' and request.user.is_authenticated(): comment = Comment(user=request.user) form = CommentForm(request.POST, request.FILES, instance=comment) if form.is_valid(): form.save() # ... # forms.py class CommentForm(forms.ModelForm): class Meta: model = Comment

但是有一些错误:

没有字段被填充 我不想将count_votes默认为0 用户也不会被考虑在内 据说这张照片也是空的

我怎样才能做到这一点? 我已经阅读了很多关于SO的问题,并尝试了其他各种各样的事情,比如填写表单__init__中的内容,使用initial而不是instance ,...

In Django, the user can upload a comment with the image.

from sorl.thumbnail import ImageField class Comment(models.Model): count_votes = models.Integer(default=0) user = models.ForeignKey(User) thumb = ImageField(upload_to="thumbnails") # ...

This is what I am trying to do :

# views.py def add_comment(request): if request.method == 'POST' and request.user.is_authenticated(): comment = Comment(user=request.user) form = CommentForm(request.POST, request.FILES, instance=comment) if form.is_valid(): form.save() # ... # forms.py class CommentForm(forms.ModelForm): class Meta: model = Comment

But there are some errors :

none of the fields are filled the count_votes is not defaulted to 0 as I would like to the user is not taken into account either the image is said to be empty too

How can I achieve that ? I have read many questions on SO and tried various other things, like fill in things in the __init__ of the form, use initial instead of instance, ...

最满意答案

首先,在模板中确保您的<form>标签中包含enctype="multipart/form-data" ,否则图像文件将无法上传,您的表单将无法验证(因此,不会将任何内容添加到数据库中)。

此外,您需要修复您的视图。 首先使用login_required装饰器,以便您的视图仅限于登录用户,然后修复表单逻辑:

from django.shortcuts import redirect, render from django.contrib.auth.decorators import login_required @login_required def add_comment(request): form = CommentForm(request.POST or None, request.FILES or None) if form.is_valid(): obj = form.save(commit=False) # create the record, but don't save it obj.user = request.user # add the user from the request obj.save() # now save the record return redirect('/') return render(request, 'template.html', {'form': form})

最后,在您的表单中排除用户,因为您稍后会添加它。 实际上,您的表单应该只有注释和图像字段。 您不需要包含count_votes字段,因为它已经有一个默认值; 除非您希望用户修改此字段。

class CommentForm(forms.ModelForm): class Meta: model = Comment fields = ('thumb', 'comment',)

First, make sure in your template you have enctype="multipart/form-data" in your <form> tag, otherwise the image file will not get uploaded and your form will not validate (and thus, nothing will be added to the database).

In addition, you need to fix your views. Start by using the login_required decorator so that your view is restricted to logged-in users, and then fix your form logic:

from django.shortcuts import redirect, render from django.contrib.auth.decorators import login_required @login_required def add_comment(request): form = CommentForm(request.POST or None, request.FILES or None) if form.is_valid(): obj = form.save(commit=False) # create the record, but don't save it obj.user = request.user # add the user from the request obj.save() # now save the record return redirect('/') return render(request, 'template.html', {'form': form})

Finally, in your form exclude the user because you will be adding it later. In fact, your form should just have the comment and image field. You don't need to include the count_votes field because it already has a default value; unless you want the user to modify this field.

class CommentForm(forms.ModelForm): class Meta: model = Comment fields = ('thumb', 'comment',)

更多推荐

本文发布于:2023-07-26 05:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   ImageField   Django   form   User

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!