AlexBrand
AlexBrand

Reputation: 12399

Accessing request context in template without view - django

I have the following view that displays the user profile:

@login_required
def user_profile(request):
    return render_to_response('registration/profile.html',context_instance=RequestContext(request))

I access the user information through the request.user variable in the template.

I was thinking that it might be easier to just have a direct_to_template url, but then the request context won't be there.

Is there a better way of doing this? Thanks

Upvotes: 0

Views: 804

Answers (1)

jpic
jpic

Reputation: 33420

Another way is using TemplateView:

url(r'^...$', TemplateView.as_view(template_name='registration/profile.html'), name='...'),

However, according to the source code of direct_to_template:

def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
    """ 
    Render a given template with any extra URL parameters in the context as
    ``{{ params }}``.
    """
    if extra_context is None: extra_context = {}
    dictionary = {'params': kwargs}
    for key, value in extra_context.items():
        if callable(value):
            dictionary[key] = value()
        else:
            dictionary[key] = value
    c = RequestContext(request, dictionary)
    t = loader.get_template(template)
    return HttpResponse(t.render(c), content_type=mimetype)

direct_to_template uses RequestContext.

Upvotes: 3

Related Questions