django username in url, instead of id
Posted
by dana
on Stack Overflow
See other posts from Stack Overflow
or by dana
Published on 2010-06-10T09:16:20Z
Indexed on
2010/06/10
9:32 UTC
Read the original article
Hit count: 324
Hello, in a mini virtual community, i have a profile_view function, so that i can view the profile of any registered user. The profile view function has as a parameter the id of the user wich the profile belongs to, so that when i want to access the profile of user 2 for example, i call it like that: http://127.0.0.1:8000/accounts/profile_view/2/
My problem is that i would like to have the username in the url, and NOT the id. I try to modify my code as follows, but it doesn't work still. Here is my code:
view:
def profile_view(request, user):
u = User.objects.get(pk=user)
up = UserProfile.objects.get(created_by = u)
cv = UserProfile.objects.filter(created_by = User.objects.get(pk=user))
blog = New.objects.filter(created_by = u)
replies = Reply.objects.filter(reply_to = blog)
vote = Vote.objects.filter(voted=blog)
following = Relations.objects.filter(initiated_by = u)
follower = Relations.objects.filter(follow = u)
return render_to_response('profile/publicProfile.html', {
'vote': vote,
'u':u,
'up':up,
'cv': cv,
'ing': following.order_by('-date_initiated'),
'er': follower.order_by('-date_follow'),
'list':blog.order_by('-date'),
'replies':replies
},
context_instance=RequestContext(request))
and my url:
urlpatterns = patterns('',
url(r'^profile_view/(?P<user>\d+)/$',
profile_view,
name='profile_view'),
thanks in advance!
© Stack Overflow or respective owner