0

因此,我按照http://www.tangowithdjango.com/book/chapters/login.html的指导设置了个人资料图片上传。然后我发现第二个答案(由用户 Raisins 提供)对我的目的有用,并在 Django 中使用自定义字段扩展用户模型。正如我一直认为这个答案已经过时一样,我也尝试了这里提供的迁移解决方案在迁移到 Django 1.6 时摆脱 get_profile()并没有改善我的情况

然后在我的视图中,我将所需的详细信息(包括图像)添加到字典中,并将 i 渲染到 HTML 页面。看起来 UserProfile 没有返回正确的对象。

u=User.object.get(username__exact=request.user)
profile=UserProfile.objects.get(user=u)
global client
client['login']=True
client['name']=u.username
client['password']=u.password
client['email']=u.email
client['picture']=profile.picture
return render(request,'index.html',{'client':client})

当我尝试在 HTML 页面上显示详细信息时,除了图像之外的所有内容都已加载。我试过了

<img src="profile_images/{{client.picture}}">

其中 profile_images 位于根目录中。我检查了元素,发现了这个

<img src="/static/profile_images/%7B%7B%20client.picture%20%7D%7D">

我期待“1.jpg”而不是“%7B%7B%20client.picture%20%7D%7D”。

任何帮助表示赞赏。谢谢

4

2 回答 2

0

我通过使用解决了我的问题

{% load staticfiles %}
<img src="{% static client.picture %}"  />
于 2014-06-12T12:27:23.470 回答
0

葡萄干的答案已经过时了。请不要再在 Django 1.6 中使用 AUTH_PROFILE_MODULE。事实上,您甚至不需要处理post_save信号。

基于相同的教程,您将找到您的视图的代码:

u = User.objects.get(username=request.user)

try:
    p = UserProfile.objects.get(user=u)
except:
    p = None

return render(request,'index.html',{'client':client, 'userprofile':p})

在您的模板中使用:

    {% if userprofile.picture %}
        <img src="{{ userprofile.picture.url }}"  />
    {% endif %}
于 2014-06-12T06:40:02.113 回答