我正在尝试显示注册到网站的新用户的个人资料图片,以便向登录到网站的每个用户显示,到目前为止,我已经能够显示他们的用户名,但我很难显示存储在网站中的个人资料图片扩展模型用户配置文件如何实现这一点以在我的模板中显示它。欢迎使用更好的解决方案
Profile_tag
def person(context, e):
"""
Renders a single user object.
"""
to_return = {
'user': context ['user'],
'profile': context['UserProfile.objects.all'],
}
register = template.Library()
register.inclusion_tag('profile/person.html', takes_context=True)(person)
Person.html
<div class="person">
<a href="{% url 'profile_detail' user.username %}">
<span class="username">{{ user.username|slice:"12" }}</span>
<p> <img src="/static/assets/{{profile_picture}}" height="100" width="100"></p>
</a>
</div>
user_list.html
{% load profile_tags %}
{% block main_content %}
<h1>Newest Users</h1>
{% friends_for_user user as friend_dict %}
{% for person in object_list %}
{% dict_entry_for_item person.username from friend_dict as friend %}
{% person person %}
{% ifnotequal person user %}
<form method="POST" action="{% if friend %}{% url 'sg_unfollow' person.username %}{% else %}{% url 'sg_follow' person.username %}{% endif %}">{%csrf_token%}
<input type="submit" value="{% if friend %}Unfollow{% else %}Follow{% endif %}" />
</form>
{% endifnotequal %}
{% endfor %}
Urls.py
urlpatterns = patterns('profile.views',
url(r'^detail/(?P<username>[a-zA-Z0-9_-]+)/$', 'detail',
name='profile_detail'),
)
答案 0 :(得分:0)
profile_picture
来自哪里?
扩展用户模型Profile
用户Foreignkey
,User
然后
models.py
class Profile(models.Model):
user = models.ForeignKey(User)
profile_picture = models.ImageField(upload_to='uploads/avatar')
views.py
userDetail = Profile.objects.get(user_id=request.session['_auth_user_id'])
templateVar['profile_picture'] = userDetail.profile_picture