Django jQuery 查看用户问题

发布于 2024-09-14 01:51:30 字数 1015 浏览 12 评论 0原文

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

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

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

通过此我正在使用jQuery选项卡 >

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

wallphotos

地址栏在用户个人资料上显示以下内容 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 照片、最近的墙壁物品等。

I seem to be stuck and am not sure which is the best direction to take.

I have a few apps in my project and would like to combine three views into one template.

I have a userprofile in which I would like to display his info, latest news feeds and also his photos

Through this I am making use of jQuery tabs

I have defined my three tabs out of which one calls on a regular div and the other two are urls that get called in.

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

the address bar reads the following when on a users profile
http://localhost:8000/profiles/profile_name/

in my views.py for wall and photos looks like the following

@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))

But if I then look at MY profile it is fine, but whenever I switch to another users profile it sill seems to display some of my information.

I know that request.user is looking at the logged in user, how do I obtain that user in the address bar and pass it on so it displays the correct info ie if profile_name = john then displays johns photos, recent wall items etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2024-09-21 01:51:30

如果你有一个像这样的urls.py

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

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

@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绑定到profiles/ 之后和最终 / 之前的 URL 位。给定 URL /profiles/john/,您最终会调用 index 视图,并将 prof_name 设置为 john

, 'appname.views.index'))

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

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

If you've got a urls.py like this:

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

Then your view code can be modified like this:

@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))

What this does is bind the name prof_name to whatever value is in the bit of the URL after profiles/ and before the final /. Given the URL /profiles/john/, you'd end up with the index view being called, with prof_name set to john.

, 'appname.views.index'))

Then your view code can be modified like this:

What this does is bind the name prof_name to whatever value is in the bit of the URL after profiles/ and before the final /. Given the URL /profiles/john/, you'd end up with the index view being called, with prof_name set to john.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文