'profile_picture'属性没有与之关联的文件

时间:2018-07-13 00:00:00

标签: django attributes

我正在尝试创建个人资料访问权限,但是我的模型属性之一(profile_picture)为空,这导致我的个人资料页面崩溃。我设置了blank=True,它已经开始工作了,但是此后就停止工作了,我不知道为什么。如果我转到django管理员并手动添加个人资料,则可以再次访问我的个人资料,一切正常。我想我的问题是,即使profile_pic属性为空,为什么我也不能查看我的个人资料? blank=True是否应该解决这个问题?

models.py

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

# Create your models here.
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=100, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)
    profile_picture = models.ImageField(upload_to='profile_pics', blank=True)

    def __str__(self):
        return self.user.username

views.py

def edit_user_profile(request):
    if request.method == 'POST':
        form = EditUserProfileForm(request.POST, instance=request.user.userprofile)
        if form.is_valid():
            form.save()

            return redirect('/accounts/profile')
    else:
        form = EditUserProfileForm(instance=request.user)
        args = {'form': form}
        return render(request, 'accounts/edit_user_profile.html', args)

Profile.html

<div class="container">
  <br>
  <h2>{{ user }}</h2>
  <br>

  <p>Name: {{ user.first_name }} {{ user.last_name }}</p>
  <img src="{{ user.userprofile.profile_picture.url }}" width="240px">
  <p></p>
  <p>Motto: {{ user.userprofile.description }}</p>
  <p>Youtube: <a href="{{ user.userprofile.website }}">{{ user.userprofile.website }}</a></p>
  <p>About Me: {{ user.userprofile.city }}</p>
  <p>Phone Number: {{ user.userprofile.phone }}</p>


  <a href="{% url 'edit_profile' %}">Edit Profile</a><br>


<!-- if profile is updated succesfully -->
 {% if messages %}
 {% for message in messages %}

 <br><br>{{ message }}

 {% endfor %}
 {% endif %}

</div>

同样,如果我使用Django管理员并手动上传照片,我可以访问我的个人资料,但是默认情况下,如果没有图片,它将无法正常工作。

任何见解都值得赞赏 谢谢

2 个答案:

答案 0 :(得分:0)

这是因为模板中的代码{{ user.userprofile.profile_picture.url }}

即使profile_picture为none,它也总是尝试查找图片的url。

只需在模板中添加if/else,如果您有默认图片,则添加方法。

即。如果/其他在模板中

  {% if user.userprofile.profile_picture %}
      <img src="{{ user.userprofile.profile_picture.url }}" width="240px">
  {% else %}
      <img src="{% static 'your_default_img_path' %}" width="240px">
  {% endif %}

或使用图片方法

# Create your models here.
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=100, default='')
    city = models.CharField(max_length=100, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)
    profile_picture = models.ImageField(upload_to='profile_pics', blank=True)

    def __str__(self):
        return self.user.username

    def get_profile_picture(self):
        if self.profile_picture
            return profile_picture_url
        else:
            return 'your_default_img_url_path'

答案 1 :(得分:0)

在views.py中尝试此代码

form = EditUserProfileForm(request.POST, request.FILES)
          if form.is_valid():
              city = form.cleaned_data['city']
              description = form.cleaned_data['description']
              profile_picture = form.cleaned_data['profile_picture']
              website = form.cleaned_data['website']
              phone = form.cleaned_data['phone']
              EditUser= UserProfile(city =city,
                                   description=description,
                                   profile_picture =profile_picture,
                                   website =website,
                                   phone =phone,
                                   user_id=request.user)
              EditUser.save()
              return redirect('/accounts/profile')

          else:
              form = EditUserProfileForm(instance=request.user)
              args = {'form': form}
              return render(request, 'accounts/edit_user_profile.html', args)