Generic detail view UserProfileDetailView must be called with either an object pk or a slug in the URLconf

前端 未结 3 1334
长发绾君心
长发绾君心 2021-01-22 16:37

Well I am facing error and it\'s now its been two days to this question and still stuck on this mistake, anybody can help and able to fix this. I am new in Django and need help.

3条回答
  •  后悔当初
    2021-01-22 17:02

    you need to add the user pk or slug in the url so that django can retrieve the user using this pk or slug so edit the url to be like this

    path('/',UserProfileDetailView.as_view(),name = 'detail'),
    

    but make sure that your slug equal the username of the user , to do this override the save method in your model to be like this

    def save(self, *args, **kwargs): 
        self.slug = self.username
        super(Your_Model_Name, self).save(*args, **kwargs)  
    

    make sure to change 'Your_Model_Name' with your model class name

提交回复
热议问题