【问题标题】:Django jQuery views user questionDjango jQuery 查看用户问题
【发布时间】:2011-03-29 09:49:16
【问题描述】:

我似乎被困住了,不确定哪个是最好的方向。

我的项目中有几个应用程序,想将三个视图合并到一个模板中。

我有一个用户个人资料,我想在其中显示他的信息、最新新闻提要以及他的照片

通过这个,我正在使用 jQuery 选项卡

我已经定义了我的三个选项卡,其中一个调用常规 div,另外两个是调用的 url。

<a href="wall/recent">wall</a><a href="photos/recent">photos</a>

在用户个人资料上时,地址栏会显示以下内容 http://localhost:8000/profiles/profile_name/

在我的views.py 中,wallphotos 如下所示

@login_required
def index(request, template_name='wall/_index.html'):
    photos = Photos.objects.filter(user=request.user).order_by('-id')
    context = { 'photos': photos, }

    return render_to_response(template_name, context,
        context_instance=RequestContext(request))

但是,如果我再看一下我的个人资料,那就没问题了,但是每当我切换到其他用户的个人资料时,它似乎仍然会显示我的一些信息。

我知道 request.user 正在查看已登录的用户,我如何在地址栏中获取该用户并将其传递给它以显示正确的信息,即如果 profile_name = john 然后显示 johns 照片,最近墙壁物品等。

【问题讨论】:

    标签: jquery django-templates django-views jquery-tabs


    【解决方案1】:

    如果你有这样的urls.py

    urlpatterns = patterns('',
                          (r'^profiles/(?P<prof_name>[A-Za-z0-9\-_]+)/$', 'appname.views.index'))
    

    那么你的视图代码可以这样修改:

    @login_required
    def index(request, prof_name, template_name='wall/_index.html'):
        photos = Photos.objects.filter(user__username=prof_name).order_by('-id')
        context = { 'photos': photos, }
    
        return render_to_response(template_name, context,
            context_instance=RequestContext(request))
    

    它的作用是将名称prof_name 绑定到URL 中profiles/ 之后和最终/ 之前的位中的任何值。给定 URL /profiles/john/,您最终会调用 index 视图,并将 prof_name 设置为 john

    【讨论】:

    • @ApPeL - 太好了,感谢您接受我的回答 - 你也介意投票吗?
    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2015-05-14
    • 1970-01-01
    相关资源
    最近更新 更多