0

The following in part of my view file

def user_detail(request, username,
    template_name='auth/user_detail.html', extra_context=None):
    context = {
        'user': shortcuts.get_object_or_404(User, username=username)
    }
    context.update(extra_context or {})
    return shortcuts.render(request, template_name, context)

and a part of my template is

 <ul class="nav">
    {% if request.user.username != 'steve' %}
    <li>
        <a href="{% url 'user_detail' 'steve' %}">Steve</a>
    </li>
    {% endif %}
    {% if request.user.username != 'james' %}
    <li>
        <a href="{% url 'user_detail' 'james' %}">James</a>
    </li>
    {% endif %}
</ul>

However even If I login using either of these users, I still get this link displayed. This is because I am checking request.user.username and the request variable is not being passed to the template by the render function.

If I directly refer as user.username then it is fine, but that is not how I want to solve this problem because there are other similar views which return this request variable that I have to use.

This is all a part of yourlabs fork of django-subscription. I have not used the render shortcut before so I do not know why I am getting this error.

Any help will be be deeply appreciated.

1 Answer 1

2

All render does is ensure that the templates are rendered with a RequestContext. This, in turn, just means that the context processors are run.

The request context processor is not enabled by default. You have to add it to settings.CONTEXT_PROCESSORS yourself if you need. However, in your case you don't actually need it, because you're just interested in the user - and the auth processor provides this as the user variable.

So, to summarise, change request.user to just user and all will be well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info.... But as I have written above this is just a particular example... I know I can access the user directly but I need to access this request context processor in other templates as well. Thanks a lot for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.