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 th
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.For Django 2.0 and above:
path(‘profile/<username>/, views.profile, name=‘profile’),
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.
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'),
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.