Issue
I am trying to customize the LoginView to user my Form That I created but getting an error when trying to enter the login page. I am getting this TypeError: init() got an unexpected keyword argument 'request'.
Traceback (most recent call last):
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/contrib/auth/views.py", line 63, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/generic/edit.py", line 133, in get
return self.render_to_response(self.get_context_data())
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/contrib/auth/views.py", line 96, in get_context_data
context = super().get_context_data(**kwargs)
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/generic/edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "/mnt/c/Users/Ghosted/Desktop/projects/helloWorld/lib/python3.7/site-packages/django/views/generic/edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: __init__() got an unexpected keyword argument 'request'
This is the Form Created
class LoginForm(forms.Form):
username = forms.CharField(label='',
widget=forms.TextInput(
attrs = {
'placeholder': 'username or email',
}
))
password = forms.CharField(label='',
widget=forms.PasswordInput(
attrs = {
'placeholder': 'password'
}
))
And I want to use it in the LoginView in django.contrib.auth.views
In views.py
from .forms import LoginForm
from django.contrib.auth.views import LoginView
class CustomLoginView(LoginView):
template_name = 'login.html'
form_class = LoginForm
In urls.py:
path('login/', views.CustomLoginView.as_view(), name='login'),
Solution
The LoginView
[Django-doc] is a view that uses a form that has been modified. That form uses a request
parameter. You thus can not simply replace it with any other form.
You can however subclass form the AuthenticationForm
[Django-doc], and then it will normally work, since you only modified the fields:
from django import forms
from django.contrib.auth.forms import AuthenticationForm
class LoginForm(AuthenticationForm):
username = forms.CharField(
label='',
widget=forms.TextInput(
attrs = {
'placeholder': 'username or email',
}
)
)
password = forms.CharField(
label='',
widget=forms.PasswordInput(
attrs = {
'placeholder': 'password'
}
)
)
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.