Answer the question
In order to leave comments, you need to log in
How to pass url slug parameter to DetailView?
I'm trying to make a user page that would access the page by username
urls.py in the main folder
urlpatterns = [
#others
path('user/', include('user.urls')),
]
from django.urls import path
from user.views import *
urlpatterns = [
path('<slug:username>/', UserView.as_view(), name='userview'),
]
from mymodels.models import CustomUser
from django.views.generic.detail import DetailView
class UserView(DetailView):
model = CustomUser
template_name = 'user/user.html'
urlpatterns = [
path('<int:pk>/', UserView.as_view(), name='userview'),
]
Answer the question
In order to leave comments, you need to log in
I highly recommend studying the sources of these views, the parameters specified in the path are in self.kwargs
PS Since I changed the question, in addition, you need to override the get_object method
It was necessary to override the get_object method. It can be omitted only if pk is used because it is used by default.
Minimum solution:
class UserView(DetailView):
model = CustomUser
template_name = 'user/user.html'
def get_object(self):
pass
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question