开发者

Django URL View parameter help

I'm having some trouble trying to pass a string from a URL to the views. My page has开发者_运维知识库 user pages, and if you go to site.com/username it brings up that users page. But when I try going it says this: [invalid literal for int() with base 10: 'username'].

Here's my code:

urls.py:

(r'^user/(?P<userName>[^/]+)/$', 'mischief_blog.mischief_app.views.user_view'),

views.py:

def user_view(request, userName):
    postList = userPost.objects.filter(author=userName)
    return render_to_response('user.html', {"user": user_name, "postList": postList}, context_instance=RequestContext(request))


get name from url:

(r'^user/(?P<userName>\w+)/$', 'mischief_blog.mischief_app.views.user_view'),

get user from name, then get posts from user: (get_object_or_404) and (User)

from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User

def user_view(request, userName=None):
    user = get_object_or_404(User, username=userName)
    postList = userPost.objects.filter(author=user)
    return render_to_response('user.html', {"user": user, "postList": postList}, context_instance=RequestContext(request))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜