Django ImageField 不会在基于函数的视图中上传,但它会在管理员中上传
Django ImageField won't upload in function based view, but it does in the admin
我一直在尝试将一些用户上传的个人资料图片添加到我的网站。 当我从管理员那里执行时它工作正常,显示图像并且所有引擎似乎都工作正常(图像转到正确的上传位置等)。问题是当我尝试从我的角度做同样的事情时。
我注意到 print("upload_location") 只有在我从管理员那里执行时才会出现。奇怪的是,我的个人资料模型中的所有其他字段都工作正常(例如名称 "foo" 已更新为 "foobar"),而不仅仅是在管理员中,但在视图中也是如此。 问题仅出在 ImageField 上。
我相信这可能与我处理 form.is_valid() 的方式有关,但我一直在研究它并且没有任何改变(我知道它在某种程度上起作用,因为 HttpResponseRedirect 正在工作。
有什么想法吗?
views.py
...
@login_required
def profile_update(request, username=None):
obj = get_object_or_404(User, username=username)
user = obj.profile
form = ProfileForm(request.POST or None, instance = user)
context = {
"form": form
}
if form.is_valid():
form.save()
return HttpResponseRedirect('/profiles/{username}'.format(username=user.user))
template = 'profile_update.html'
return render(request, template, context)
forms.py
from django import forms
from .models import Profile
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"profilePic",
"nome",
...
]
def profile(self, request, user):
print('printing forms')
user.uf = self.cleaned_data['uf']
user.cidade = self.cleaned_data['cidade']
user.telefone = self.cleaned_data['telefone']
user.save()
models.py
...
User = settings.AUTH_USER_MODEL # 'auth.User'
def upload_location(instance, filename):
print("upload_location")
return "%s/%s" %(instance.user, filename)
class Profile(models.Model):
user = models.OneToOneField(User)
id = models.AutoField(primary_key=True)
width = models.IntegerField(default=0, null=True, blank=True,)
height = models.IntegerField(default=0, null=True, blank=True,)
profilePic = models.ImageField(
upload_to = upload_location,
blank=True, null=True,
verbose_name = 'Foto de Perfil',
width_field="width",
height_field="height",
)
...
template.html
...
<form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Enviar" class="btn btn-primary"/>
</form>
...
您需要将文件添加到表单中。
form = ProfileForm(request.POST or None, request.FILES or None, instance = user)
文档:https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/
我一直在尝试将一些用户上传的个人资料图片添加到我的网站。 当我从管理员那里执行时它工作正常,显示图像并且所有引擎似乎都工作正常(图像转到正确的上传位置等)。问题是当我尝试从我的角度做同样的事情时。
我注意到 print("upload_location") 只有在我从管理员那里执行时才会出现。奇怪的是,我的个人资料模型中的所有其他字段都工作正常(例如名称 "foo" 已更新为 "foobar"),而不仅仅是在管理员中,但在视图中也是如此。 问题仅出在 ImageField 上。
我相信这可能与我处理 form.is_valid() 的方式有关,但我一直在研究它并且没有任何改变(我知道它在某种程度上起作用,因为 HttpResponseRedirect 正在工作。
有什么想法吗?
views.py
...
@login_required
def profile_update(request, username=None):
obj = get_object_or_404(User, username=username)
user = obj.profile
form = ProfileForm(request.POST or None, instance = user)
context = {
"form": form
}
if form.is_valid():
form.save()
return HttpResponseRedirect('/profiles/{username}'.format(username=user.user))
template = 'profile_update.html'
return render(request, template, context)
forms.py
from django import forms
from .models import Profile
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"profilePic",
"nome",
...
]
def profile(self, request, user):
print('printing forms')
user.uf = self.cleaned_data['uf']
user.cidade = self.cleaned_data['cidade']
user.telefone = self.cleaned_data['telefone']
user.save()
models.py
...
User = settings.AUTH_USER_MODEL # 'auth.User'
def upload_location(instance, filename):
print("upload_location")
return "%s/%s" %(instance.user, filename)
class Profile(models.Model):
user = models.OneToOneField(User)
id = models.AutoField(primary_key=True)
width = models.IntegerField(default=0, null=True, blank=True,)
height = models.IntegerField(default=0, null=True, blank=True,)
profilePic = models.ImageField(
upload_to = upload_location,
blank=True, null=True,
verbose_name = 'Foto de Perfil',
width_field="width",
height_field="height",
)
...
template.html
...
<form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Enviar" class="btn btn-primary"/>
</form>
...
您需要将文件添加到表单中。
form = ProfileForm(request.POST or None, request.FILES or None, instance = user)
文档:https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/