[英]Can't Upload Image In Django
I have a profile page where I would like to allow a user to upload a profile picture.我有一个个人资料页面,我想允许用户上传个人资料图片。 I can edit all of the text but cannot upload an image.我可以编辑所有文本,但无法上传图片。 It works if i add the image via the Admin, but not via the user's profile page on the website.如果我通过管理员添加图像而不是通过网站上的用户个人资料页面添加图像,它会起作用。 Note that when created via admin--it is uploading correctly to the directory (profile_image) that i've specified in media folder.请注意,通过管理员创建时 - 它正确上传到我在媒体文件夹中指定的目录(profile_image)。 I've created some error handling on template page, but the error generated is this: "The 'image' attribute has no file associated with it."我在模板页面上创建了一些错误处理,但生成的错误是这样的:“'image' 属性没有与之关联的文件。” Below is my code:下面是我的代码:
models.py模型.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(default='',max_length=100 )
last_name = models.CharField(default='',max_length=100)
email = models.CharField(max_length=100, default='')
date_birth = models.DateField(default=datetime.datetime.now())
bio = models.TextField(default='')
image = models.ImageField(upload_to='profile_image', blank=True)
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)
views.py视图.py
@login_required
def edit_profile(request):
profile = get_object_or_404(models.UserProfile)
if request.method == 'POST':
form = forms.EditProfileForm(data=request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect('/accounts/profile')
else:
form = forms.EditProfileForm(instance=profile)
args = {'form':form}
return render(request, 'accounts/edit_profile.html', args)
forms.py表格.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm
from . import models
class UserProfileForm(forms.ModelForm):
class Meta:
model = models.UserProfile
fields = [
'first_name',
'last_name',
'email',
'date_birth',
'bio',
'image',
]
class EditProfileForm(UserProfileForm):
model = models.UserProfile
fields = [
'first_name',
'last_name',
'email',
'date_birth',
'bio',
'image',
]
settings.py设置.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'accounts/media')
edit_profile.html edit_profile.html
{% extends "layout.html" %}
{% block title %}User Profile | {{ user }}{{ super }}{% endblock %}
{% block body %}
<h1>My Profile</h1>
<form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p}}
<button type="submit">Save</button>
</form>
{% endblock %}
The file comes in request.FILES
, not request.POST
. 该文件位于request.FILES
,而不是request.POST
。 Do the following in the view: 在视图中进行以下操作:
form = forms.EditProfileForm(data=request.POST, files=request.FILES, instance=profile)
See the documentation on file uploads : 请参阅有关文件上传的文档 :
A view handling this form will receive the file data in
request.FILES
, which is a dictionary containing a key for eachFileField
(orImageField
, or otherFileField subclass
) in the form. 处理此表单的视图将在request.FILES
接收文件数据,request.FILES
是一个字典,其中包含该表单中每个FileField
(或ImageField
或其他FileField subclass
)的键。
Please check your folder permissions also , DRF is not throwing any permission errors for me.还请检查您的文件夹权限,DRF 不会为我抛出任何权限错误。
For Mac use : sudo chmod 777 *
For Live Server don't use this just check what is relevant and secure for your Os.对于 Mac 使用: sudo chmod 777 *
对于 Live Server,不要使用它,只需检查与您的操作系统相关且安全的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.