royaIT
royaIT

Reputation: 75

Link an user to his profile page

How do I link the logged in user to his profile page?

{% if user.is_authenticated %}

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

Here are the involved parts:

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

Upvotes: 1

Views: 1057

Answers (1)

v1k45
v1k45

Reputation: 8250

In you template you are trying to use url tag with named urls even though you haven't passed name keyword argument to url function in your urlpatterns.

In your urls function pass name argument, like this:

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

make sure you namespaced the app as 'blog' in your root url conf.

In your template to access current user's profile id by request context's user object. Like this:

{% if user.is_authenticated %}

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

Upvotes: 2

Related Questions