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 }}
.
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)
- [Django]-How can I create sophisticated Django Model Validation for Django Admin?
- [Django]-How to read Django request.FILES (which is an image) as binary
- [Django]-How to set headers in DRF's APIClient() delete() request?
- [Django]-How to restrict access for staff users to see only their information in Django admin page?
- [Django]-Django Forms for generic relationships. How to include them?
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),
)
- [Django]-File does not exist: /var/www/polls
- [Django]-Populate CheckboxSelectMultiple with existing data from django model form
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.
- [Django]-Sudo /etc/init.d/celeryd start generates a "Unknown command: 'celeryd_multi'"
- [Django]-Sprinkling VueJs components into Django templates
- [Django]-How do I create a user using OAuth2?
- [Django]-Serializer return incorrect output
- [Django]-ImportError: No module named books.models
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.
- [Django]-Best practice β Django multisite
- [Django]-Missing staticfiles manifest entry in Django deployment using Heroku
- [Django]-Django method to change User email not working
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.