Generic detail view ProfileView must be called with either an object pk or a slug

2024/9/24 10:19:29

I'm new to Django 2.0 and i'm getting this error when visiting my profile page view. It's working with urls like path('users/<int:id>') but i wanted to urls be like path('<username>'). Not sure what exactly is the problem. I hope you can help.

#views.py
class ProfileView(views.LoginRequiredMixin, generic.DetailView):model = models.Usertemplate_name = 'accounts/profile.html'#urls.py
urlpatterns = [path('', HomePageView.as_view(), name='home'),path('signup', SignUpView.as_view(), name='signup'),path('login', LoginView.as_view(), name='login'),path('logout', logout_view, name='logout'),path('<username>', ProfileView.as_view(), name='profile')
]#base.html
<ul class="dropdown-menu"><li><a href="{% url 'accounts:profile' user.username %}">View Profile</a></li><li><a href="#">Edit Profile</a></li>
</ul>
Answer

You need to tell your view to use username as the lookup field. You could either do this by defining slug_field and slug_url_kwarg on the model, or by overriding get_object. For example:

class ProfileView(views.LoginRequiredMixin, generic.DetailView):model = models.Usertemplate_name = 'accounts/profile.html'slug_field = 'username'slug_url_kwarg = 'username'

The first of these determines what field to use in the model lookup; the second determines what variable to use from the URL pattern.

https://en.xdnf.cn/q/71712.html

Related Q&A

Python Pandas group datetimes by hour and count row

This is my transaction dataframe, where each row mean a transaction :date station 30/10/2017 15:20 A 30/10/2017 15:45 A 31/10/2017 07:10 A 31/10/2017 07:25 B 31/10/2017 07:55 …

Get Bokehs selection in notebook

Id like to select some points on a plot (e.g. from box_select or lasso_select) and retrieve them in a Jupyter notebook for further data exploration. How can I do that?For instance, in the code below, …

Upload CSV file into Microsoft Azure storage account using python

I am trying to upload a .csv file into Microsoft Azure storage account using python. I have found C-sharp code to write a data to blob storage. But, I dont know C# language. I need to upload .csv file …

GeoDjango: How can I get the distance between two points?

My Profile model has this field:location = models.PointField(geography=True, dim=2, srid=4326)Id like to calculate the distance between the two of these locations (taking into account that the Earth is…

Reading direct access binary file format in Python

Background:A binary file is read on a Linux machine using the following Fortran code:parameter(nx=720, ny=360, nday=365) c dimension tmax(nx,ny,nday),nmax(nx,ny,nday)dimension tmin(nx,ny,nday),nmin(nx,…

Fabric/Python: AttributeError: NoneType object has no attribute partition

Have the following function in fabric for adding user accounts.~/scripts #fab -lPython source codeAvailable commands:OS_TYPEadduser_createcmd Create command line for adding useradduser_getinfo Prom…

Python object @property

Im trying to create a point class which defines a property called "coordinate". However, its not behaving like Id expect and I cant figure out why. class Point:def __init__(self, coord=None…

Tkinter - Inserting text into canvas windows

I have a Tkinter canvas populated with text and canvas windows, or widgets, created using the create_text and create_window methods. The widgets I place on the canvas are text widgets, and I want to in…

How to run nosetests without showing of my matplotlibs graph?

I try to run my test without any messages displaying from my main program. I only want verbose messages from nosetests to display.For example: nosetests -v --nologcaptureAll of my printout messages fro…

Variable name to string in Python

I have written some code in Python and I would like to save some of the variables into files having the name of the variable. The following code:variableName1 = 3.14 variableName2 = 1.09 save_variable_…