0

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

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

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

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

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

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

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

在我 views.py的 for 中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 正在查看登录用户,我如何在地址栏中获取该用户并将其传递给它以显示正确的信息,即 if profile_name= john 然后显示 johns 照片、最近的墙项目等。

4

1 回答 1

2

如果你有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))

这样做的目的是将名称绑定到 URL中 finalprof_name之后和之前的任何值。给定 URL ,您最终会调用视图,并将其设置为。profiles///profiles/john/indexprof_namejohn

于 2010-08-13T10:39:58.163 回答