我已经在基本模板中做到了这一点:
{% if user.profile.image %}
<img class="user-image" src="{{ user.profile.image.url }}/" alt="User profile picture">
{% else %}
<img class="user-image" src="{% static 'userprofile/download (1).jpg' %}">
{% endif %}
在个人资料详细信息页面中已执行以下操作:
{% if profile_details.image %}
<img class="profile-user-img img-responsive img-circle" src="{{ profile_details.image.url }}/" alt="User profile picture">
{% else %}
<img class="profile-user-img img-circle" src="{% static 'userprofile/download (1).jpg' %}">
{% endif %}
在我的模型中,我做到了:
from PIL import Image
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
import sys
class Profile(models.Model):
Date = models.DateTimeField(auto_now_add=True)
Full_Name = models.CharField(max_length=32,blank=True)
Name = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
E_mail = models.EmailField(max_length=70,blank=True)
image = models.ImageField(upload_to='user_images', null=True, blank=True)
def save(self, *args, **kwargs):
imageTemproary = Image.open(self.image)
outputIoStream = BytesIO()
imageTemproaryResized = imageTemproary.resize( (128,128) )
imageTemproaryResized.save(outputIoStream , format='JPEG', quality=150)
outputIoStream.seek(0)
self.image = InMemoryUploadedFile(outputIoStream,'ImageField', "%s.jpg" %self.image.name.split('.')[0], 'image/jpeg', sys.getsizeof(outputIoStream), None)
super(Profile, self).save(*args, **kwargs)
此代码行中出现问题:
imageTemproary = Image.open(self.image)
跟踪:
Traceback:
File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\edit.py" in post
172. return super().post(request, *args, **kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\edit.py" in post
142. return self.form_valid(form)
File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\edit.py" in form_valid
125. self.object = form.save()
File "C:\Users\HP\myEnv\lib\site-packages\django\contrib\auth\forms.py" in save
120. user.save()
File "C:\Users\HP\myEnv\lib\site-packages\django\contrib\auth\base_user.py" in save
73. super().save(*args, **kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\base.py" in save
729. force_update=force_update, update_fields=update_fields)
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\base.py" in save_base
769. update_fields=update_fields, raw=raw, using=using,
File "C:\Users\HP\myEnv\lib\site-packages\django\dispatch\dispatcher.py" in send
178. for receiver in self._live_receivers(sender)
File "C:\Users\HP\myEnv\lib\site-packages\django\dispatch\dispatcher.py" in <listcomp>
178. for receiver in self._live_receivers(sender)
File "C:\Users\HP\My Documents\github\erpcloud\erpcloud\userprofile\models.py" in user_created
82. Profile.objects.create(Name=instance)
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\query.py" in create
417. obj.save(force_insert=True, using=self.db)
File "C:\Users\HP\My Documents\github\erpcloud\erpcloud\userprofile\models.py" in save
68. imageTemproary = Image.open(self.image)
File "C:\Users\HP\myEnv\lib\site-packages\PIL\Image.py" in open
2584. fp.seek(0)
File "C:\Users\HP\myEnv\lib\site-packages\django\core\files\utils.py" in <lambda>
20. seek = property(lambda self: self.file.seek)
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\fields\files.py" in _get_file
41. self._require_file()
File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\fields\files.py" in _require_file
38. raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
Exception Type: ValueError at /accounts/signup/
Exception Value: The 'image' attribute has no file associated with it.
谁能解释我的代码有什么问题