views:

20

answers:

1

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.

+1  A: 

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

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

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.

Dominic Rodger
hey. thanks for the info it works that way around
ApPeL
@ApPeL - great, thanks for accepting my answer - would you mind upvoting it too?
Dominic Rodger