因此,我按照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”。
任何帮助表示赞赏。谢谢