django username in url, instead of id

后端 未结 5 714

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

5条回答
  •  借酒劲吻你
    2020-12-30 18:39

    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\w+)/$', 
                           profile_view,
                           name='profile_view'),
    

提交回复
热议问题