Reputation: 5208
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!
Upvotes: 6
Views: 29185
Reputation: 1151
In urls.py
urlpatterns = [
path('<str:username>/', UserDetailView.as_view(), name='user_detail'),
]
With class based view.
class UserDetailView(LoginRequiredMixin, DetailView):
model = User
slug_field = "username"
slug_url_kwarg = "username"
template_name = "account/user_detail.html"
def get_object(self):
object = get_object_or_404(User, username=self.kwargs.get("username"))
# only owner can view his page
if self.request.user.username == object.username:
return object
else:
# redirect to 404 page
print("you are not the owner!!")
I have tested in Django 2.1.
Upvotes: 7
Reputation: 409
For Django 2.0 and above:
path(‘profile/<username>/, views.profile, name=‘profile’),
Upvotes: 1
Reputation: 24844
Here's how I do it using class based generic views and the use of slugs. It's surprisingly easy and requires only a few lines of code with the help of slugs.
# accounts/views.py
from django.contrib.auth.models import User
from django.views.generic.detail import DetailView
class UserProfileView(DetailView):
model = User
slug_field = "username"
template_name = "userprofile.html"
# accounts/urls.py
from views import UserProfileView
urlpatterns = patterns('',
# By user ID
url(r'^profile/id/(?P<pk>\d+)/$', UserProfileView.as_view()),
# By username
url(r'^profile/username/(?P<slug>[\w.@+-]+)/$', UserProfileView.as_view()),
)
Now you can access both the user like accounts/profile/id/123/
as well as accounts/profile/username/gertvdijk/
.
What's happening here?
pk
URL parameter is omitted and replaced by slug
in the URL pattern. This is accepted by the view, because...User.username
by the use of model = User
and slug_field = "username"
. (docs)pk
parameter.Upvotes: 12
Reputation: 36453
Add in the top of the file:
from django.shortcuts import get_object_or_404
and replace
u = User.objects.get(pk=user)
with
u = get_object_or_404(User, <login>=user)
where login is the username field in User model.
Upvotes: 3
Reputation: 599470
You don't show what you have tried, or where you are having trouble. This is fairly simple, after all - just pass a username string to the function instead of an integer, and look up based on that.
def profile_view(request, username):
u = User.objects.get(username=username)
and modify your url to allow strings rather than integers:
url(r'^profile_view/(?P<username>\w+)/$',
profile_view,
name='profile_view'),
Upvotes: 31