简体   繁体   中英

How to pass context defined in setting TEMPLATE_CONTEXT_PROCESSORS to my custom views

I'm having this problem while using Django Social Auth. I Have this template.

{% block content %}
<div id="content-main">
    <ul>
        {% for name in social_auth.backends %}
        <li>
            <a rel="nofollow" href="{% url "socialauth_begin" name %}">{{ name|title }}</a>
        </li>
        {% endfor %}
    </ul>
</div>
{% endblock %}

which is rendered correctly if I use the standard urlpattern (it's the login page)

url(r'^accounts/login/$', 'django.contrib.auth.views.login'),

but if I use my custom view how do I pass the context which is defined in TEMPLATE_CONTEXT_PROCESSORS ?

This is my view and the entry in the settings.py file

def login(request):
    t = loader.get_template('registration/login.html')
    c = Context( {} )
    return HttpResponse(t.render(c))

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'social_auth.context_processors.social_auth_by_name_backends',
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_login_redirect',
)

it makes sense that in my view the context is empty, I pass it an empty object, what should I do to pass the correct context ?

You need to render the template using RequestContext and return the response. This is generally done as:

def someview(request):
    context = { }
    return render_to_response('some_template.html', context,
                context_instance = RequestContext(request))

More documentation at Subclassing RequestContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM