2016-03-20 52 views
0

如何将登录用户链接到他的个人资料页面?将用户链接到他的个人资料页面

{% if user.is_authenticated %} 

<a href= "{% url 'blog:profile' userProfile.user %}">Profile</a> 

下面是所涉及的部分:

views.py

@login_required 
def profile(request, profile_id): 
    if profile_id == "0": 
     if request.user.is_authenticated: 
      userProfile = UserProfile.objects.get(pk=profile_id) 
    else: 
     userProfile = UserProfile.objects.get(pk=profile_id) 

    return render_to_response('blog/profile.html', {'userProfile':userProfile}, RequestContext(request)) 

urls.py

url(r'^profile/(?P<profile_id>\d+)/$', views.profile), 

models.py

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    bio = models.TextField(max_length=500, blank = True, default=('keine Angabe'), null=True) 
    image = models.FileField(null=True, blank=True) 

    def __unicode__(self): 
     return self.user.username 
+1

您遇到的任何特定问题/错误? – v1k45

+0

例外类型:\t NoReverseMatch 例外值:\t 未找到参数'('',)'和关键字参数'{}'的'profile'反向。 0模式尝试:[] – royaIT

+0

可能会有settings.py的问题吗? – royaIT

回答

2

在您的模板中,尽管您尚未在urlpatterns中将name关键字参数传递给url函数,但您仍尝试使用url标记和指定的网址。

在网址功能通name的说法,这样的:

url(r'^profile/(?P<profile_id>\d+)/$', views.profile, name='profile'), 

确保您在根URL的conf命名空间的应用程序为“博客”。

在您的模板中,通过请求上下文的用户对象访问当前用户的配置文件ID。像这样:

{% if user.is_authenticated %} 

    <a href= "{% url 'blog:profile' user.userprofile.id %}">Profile</a> 
+0

非常感谢! :) – royaIT

相关问题