[Django]-Django – Using context_processor

4πŸ‘

βœ…

I believe OP has incorrectly assumed that the template context variable is going to match the function name of the context processor.

OP’s context processor global_login_form() injects formLogin to the template context. Therefore, in the templates the form should be referenced as, for example, {{ formLogin.as_p }}.

πŸ‘€Jay Yu

4πŸ‘

you must have the form variable in every views, or you should implement a templatetag instead. example:

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-tags

from django import template
from django.contrib.auth.forms import AuthenticationForm

register = template.Library()

@register.inclusion_tag('registration/login.html', takes_context=True)
def login(context):
    """
    the login form 
    {% load login %}{% login %}
    """
    request = context.get('request', None)
    if not request:
        return 
    if request.user.is_authenticated():
        return dict(formLogin=AuthenticationForm())
    return dict(user=request.user) 
πŸ‘€surfeurX

4πŸ‘

I want to use everywhere on my site gives a signal that you might need a template context processor.

def global_login_form(request):
    next = request.GET.get('next', '/')
    login_form = AuthenticationForm(initial={'next': next})
    return {'login_form': login_form}

Having this and adding this context processor to TEMPLATE_CONTEXT_PROCESSORS section in settings you can use {{global_login_form}} in your templates.

Probably you’ll have more or less something like this

....
<form action="{% url login_view_name %}" method="POST">
{{global_login_form.as_p}}
</form>
....

I used this pattern in one of my projects and it worked pretty fine. Only note here that in case if form is not validated it is better to display form in errors on separated page. Here’s an example of the view:

def login(request):
    if request.method == "POST":
        login_form =LoginForm(data=request.POST)
        if login_form.is_valid():
            next = login_form.cleaned_data.get('next', '/')
            login(request, login_form.get_user())
            return HttpResponseRedirect(next)

    return render_to_response(
        'users/login/login_page.html', 
        {'form': login_form},
        context_instance=RequestContext(request),
        )
πŸ‘€dzida

0πŸ‘

class MyForm(forms.Form):
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'login_text'}))
    password = forms.CharField(widget=forms.TextInput(attrs={'class':'password_text'}))

This may be your solution. Css is passed as an attribute of the form field, so you don’t need to explicitly declare it into your html.

0πŸ‘

Don’t try to put in POST something that wasn’t really POSTed. If you need to get some information to the templatetag, just use the context.

πŸ‘€rczajka

0πŸ‘

Finally I used a middleware to process the request, instead of using a context_processor. Also, I deleted my login view and I changed the form action to β€œ.” so the login functionality is in the middleware.

I followed this question to find the solution.

πŸ‘€juankysmith

Leave a comment