在url中使用django用户名,而不是id

时间:2022-09-04 12:37:13

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 the user wich the profile belongs to, so that when i want to access the profile of user 2 for example, i call it like that: http://127.0.0.1:8000/accounts/profile_view/2/

在迷你虚拟社区中,我有一个profile_view功能,以便我可以查看任何注册用户的个人资料。配置文件视图功能将配置文件所属的用户的id作为参数,因此当我想访问用户2的配置文件时,我将其称为:http://127.0.0.1:8000 /账户/ profile_view / 2 /

My problem is that i would like to have the username in the url, and NOT the id. I try to modify my code as follows, but it doesn't work still. Here is my code:

我的问题是我想在网址中输入用户名,而不是id。我尝试按如下方式修改我的代码,但它仍然不起作用。这是我的代码:

view:

视图:

def profile_view(request, user):
        u = User.objects.get(pk=user)
        up = UserProfile.objects.get(created_by = u)
        cv = UserProfile.objects.filter(created_by = User.objects.get(pk=user))
        blog = New.objects.filter(created_by = u) 
        replies = Reply.objects.filter(reply_to = blog)
        vote = Vote.objects.filter(voted=blog)
        following = Relations.objects.filter(initiated_by = u)
        follower = Relations.objects.filter(follow = u)
    return render_to_response('profile/publicProfile.html', {
        'vote': vote,
        'u':u,  
        'up':up, 
        'cv': cv, 
        'ing': following.order_by('-date_initiated'),  
        'er': follower.order_by('-date_follow'),
        'list':blog.order_by('-date'),
        'replies':replies
        }, 
        context_instance=RequestContext(request)) 

and my url:

和我的网址:

urlpatterns = patterns('',
                        url(r'^profile_view/(?P<user>\d+)/$', 
                           profile_view,
                           name='profile_view'),

thanks in advance!

提前致谢!

3 个解决方案

#1


20  

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'),

#2


9  

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.

这是我如何使用基于类的通用视图和slugs的使用。它非常简单,只需要几行代码就可以使用slug。

# 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/.

现在,您可以访问用户,例如accounts / profile / id / 123 /以及accounts / profile / username / gertvdijk /。

What's happening here?

这里发生了什么事?

  • The usually required pk URL parameter is omitted and replaced by slug in the URL pattern. This is accepted by the view, because...
  • 通常需要的pk URL参数被省略,并由URL模式中的slug替换。这被观点所接受,因为......
  • The slug parameter is being used by the DetailView (or any other SingleObjectMixin-based view) to find the object on User.username by the use of model = User and slug_field = "username". (docs)
  • sluView参数由DetailView(或任何其他基于SingleObjectMixin的视图)使用,通过使用model = User和slug_field =“username”在User.username上查找对象。 (文档)
  • Because a slug field is optional, the view is also happily serving using just the pk parameter.
  • 因为slug字段是可选的,所以视图也很乐意仅使用pk参数。

#3


2  

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.

其中login是用户模型中的用户名字段。

#1


20  

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'),

#2


9  

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.

这是我如何使用基于类的通用视图和slugs的使用。它非常简单,只需要几行代码就可以使用slug。

# 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/.

现在,您可以访问用户,例如accounts / profile / id / 123 /以及accounts / profile / username / gertvdijk /。

What's happening here?

这里发生了什么事?

  • The usually required pk URL parameter is omitted and replaced by slug in the URL pattern. This is accepted by the view, because...
  • 通常需要的pk URL参数被省略,并由URL模式中的slug替换。这被观点所接受,因为......
  • The slug parameter is being used by the DetailView (or any other SingleObjectMixin-based view) to find the object on User.username by the use of model = User and slug_field = "username". (docs)
  • sluView参数由DetailView(或任何其他基于SingleObjectMixin的视图)使用,通过使用model = User和slug_field =“username”在User.username上查找对象。 (文档)
  • Because a slug field is optional, the view is also happily serving using just the pk parameter.
  • 因为slug字段是可选的,所以视图也很乐意仅使用pk参数。

#3


2  

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.

其中login是用户模型中的用户名字段。