'CharField' object has no attribute 'as_widget'

Hey,

I’m tryining to make a custom form of the ResetPasswordView from Django.
I did everything as it should and this error appears by loading the page.

'CharField' object has no attribute 'as_widget'

heres my code

forms.py

class CaptchaPasswordResetForm(PasswordResetConfirmView):
    new_password1 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))
    new_password2 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))
    class Meta:
        model = User
        fields = ('new_password1', 'new_password2')

heres my views.py

class password_reset_view(PasswordResetConfirmView):
    form_class = CaptchaPasswordResetForm
    success_url = reverse_lazy('home:login')  

herey my urls.py (the one in the base)

from StartSite.forms import NewPasswordResetForm
from StartSite.forms import CaptchaPasswordResetForm
from django.contrib import admin
from django.urls import path, include
from django.urls.base import reverse_lazy
from django.contrib.auth import views as auth_views
from StartSite.views import PasswordResetView, password_reset_email_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('StartSite.urls')),

    path('reset_password/',
        password_reset_email_view.as_view(template_name="home/password_reset.html", form_class=NewPasswordResetForm),
        name="reset_password"),

    path('reset_password_sent/', 
        auth_views.PasswordResetDoneView.as_view(template_name="home/password_reset_sent.html"), 
        name="password_reset_done"),

    path('reset/confirm/<uidb64>/<token>/',
        PasswordResetView.as_view(template_name="home/password_reset_form.html", form_class=CaptchaPasswordResetForm), 
        name="password_reset_confirm"),

    path('reset_password_complete/', 
        auth_views.PasswordResetCompleteView.as_view(template_name="home/password_reset_done.html"), 
        name="password_reset_complete"),
]

and heres my html code (the important part):

                  <form method="post" class="id_email_change_form">
						{% csrf_token %}
						<p>Please enter your new password</p>
						<h6 class="mb-0">New Password</h6>
						<div class="pw_space2"></div>
						{% render_field form.new_password1 class="form-control" type="password" placeholder="New Password" id="phone-form7-b" data-form-field="password"%}
						<div class="pw_space"></div>
						<h6 class="mb-0">Repeat New Password</h6>
						<div class="pw_space2"></div>
						{% render_field form.new_password2 class="form-control" type="password" placeholder="Repeat New Password" id="phone-form7-b" data-form-field="password"%}
						{{form.errors}}
						<div class="pw_space2"></div>
						<button style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " type="submit">Change Password</button>
						<a style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " target="__blank" href="{%url 'home:profilesettings'%}">Cancel</a>
                </form>

I have something similar with the first form where you can just put in your email and then the links gets sended, I tried to remove the fields in the render so that theres just the render tag and the name of the form and the erorr disappeared but the form also. So the input field isnt there anymore. I think its something wrong with the way im doing this and the erorr for both is the same

What is the full stack trace containing that error?

The tag “render_field” is not a standard Django tag, which means it comes either from a third party library or it is something you’re providing. If it’s from a third party, please identify which package is supplying that. If it’s something you’ve written, please post the code for it.

You show a view named password_reset_view, but you don’t show a url referring to that view. According to the docs, the url named “password_reset_confirm” is supposed to use the standard view named PasswordResetConfirmedView, yet you have it referencing a view named PasswordResetView. The PasswordResetView is for something completely different.

Full Stack Trace

Internal Server Error: /reset/confirm/Mg/atebe7-cf1b375173ac7309287f6520a17a38a9/
Traceback (most recent call last):
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\widget_tweaks\templatetags\widget_tweaks.py", line 222, in render
    bounded_field = set_attr(
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\widget_tweaks\templatetags\widget_tweaks.py", line 13, in wrapped
    return fn(field, attr)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\widget_tweaks\templatetags\widget_tweaks.py", line 50, in set_attr
    return _process_field_attributes(field, attr, process)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\widget_tweaks\templatetags\widget_tweaks.py", line 28, in _process_field_attributes
    old_as_widget = field.as_widget
AttributeError: 'CharField' object has no attribute 'as_widget'

the render_field is from “widget_tweaks” but Im very sure that isnt the error because I used it for other things before.

I saw the error with the passwort_reset_view in the urls and changed it

from StartSite.views import password_reset_view
from StartSite.forms import NewPasswordResetForm
from StartSite.forms import CaptchaPasswordResetForm
from django.contrib import admin
from django.urls import path, include
from django.urls.base import reverse_lazy
from django.contrib.auth import views as auth_views
from StartSite.views import PasswordResetView, password_reset_email_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('StartSite.urls')),

    path('reset_password/',
        password_reset_email_view.as_view(template_name="home/password_reset.html", form_class=NewPasswordResetForm),
        name="reset_password"),

    path('reset_password_sent/', 
        auth_views.PasswordResetDoneView.as_view(template_name="home/password_reset_sent.html"), 
        name="password_reset_done"),

    path('reset/confirm/<uidb64>/<token>/',
        password_reset_view.as_view(template_name="home/password_reset_form.html", form_class=CaptchaPasswordResetForm), 
        name="password_reset_confirm"),

    path('reset_password_complete/', 
        auth_views.PasswordResetCompleteView.as_view(template_name="home/password_reset_done.html"), 
        name="password_reset_complete"),
]

now the url is reffering to my personal view for the passwort reset but still the same error appears.

There are a couple things I noticed:

This is supposed to be a form, why is it inheriting from a View?

This isn’t a Model form. You’ve got a basic form with two fields. There are no fields in the standard User model named ‘new_password1’ and ‘new_password2’. The entire Meta section can be removed.

In your template:

You’ve got two elements with the same id attribute - a no-no in any html document.

so this worked I used views instead of forms in the parameter by mistake, the first form is working now, the second one prints this

Internal Server Error: /reset/confirm/MQ/ate9ub-fb776ac216cb82185f9be9e65aa68574/
Traceback (most recent call last):
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 284, in dispatch
    return self.render_to_response(self.get_context_data())
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 308, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 200, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
    kwargs['form'] = self.get_form()
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\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 'user'
[23/Sep/2021 21:19:35] "GET /reset/confirm/MQ/ate9ub-fb776ac216cb82185f9be9e65aa68574/ HTTP/1.1" 500 107446
[23/Sep/2021 21:19:35] "GET /settings/ HTTP/1.1" 200 6666
[23/Sep/2021 21:19:37] "GET /settings/language/ HTTP/1.1" 200 3735
[23/Sep/2021 21:19:37] "GET /static/CSS/languagesettings.css HTTP/1.1" 200 1124
Internal Server Error: /reset/confirm/MQ/ate9ub-fb776ac216cb82185f9be9e65aa68574/
Traceback (most recent call last):
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 284, in dispatch
    return self.render_to_response(self.get_context_data())
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 308, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\views.py", line 200, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
    kwargs['form'] = self.get_form()
  File "C:\Users\Finn\AppData\Local\Programs\Python\Python39\lib\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 'user'

forms

class CaptchaPasswordResetForm(PasswordResetForm):
    new_password1 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))
    new_password2 = forms.forms.CharField(max_length=100, widget=forms.forms.PasswordInput(attrs={"class": "form-control", 'type':'password'}))

views

class password_reset_view(PasswordResetConfirmView):
    form_class = CaptchaPasswordResetForm
    success_url = reverse_lazy('home:login')  

html

                  <form method="post" class="id_password_reset_form">
                      {% csrf_token %}
                      <p>Please enter your new password</p>
                      <h6 class="mb-0">New Password</h6>
                      <div class="pw_space2"></div>
                      {% render_field form.new_password1 class="form-control" type="password" placeholder="New Password" data-form-field="password"%}
                      <div class="pw_space"></div>
                      <h6 class="mb-0">Repeat New Password</h6>
                      <div class="pw_space2"></div>
                      {% render_field form.new_password2 class="form-control" type="password" placeholder="Repeat New Password" data-form-field="password"%}
                      {{form.errors}}
                      <div class="pw_space2"></div>
                      <button style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " type="submit">Change Password</button>
                      <a style="background-color:#7231aa; border:#7231aa; color:white;" class="btn btn-secondary " target="__blank" href="{% url 'home:profilesettings' %}">Cancel</a>
                </form>