Internal Server Error: /admin/account/customuser/add/
Traceback (most recent call last):
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\options.py", line 642, in get_form
return modelform_factory(self.model, **defaults)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\forms\models.py", line 543, in modelform_factory
return type(form)(class_name, (form,), form_class_attrs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\forms\models.py", line 252, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (password2, password1) specified for CustomUser
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\options.py", line 544, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\sites.py", line 211, in inner
return view(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Andriken\AppData\Local\Programs\Python\Python35\Lib\contextlib.py", line 30, in inner
return func(*args, **kwds)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\auth\admin.py", line 128, in add_view
extra_context)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\options.py", line 1509, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Andriken\AppData\Local\Programs\Python\Python35\Lib\contextlib.py", line 30, in inner
return func(*args, **kwds)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\options.py", line 1438, in changeform_view
ModelForm = self.get_form(request, obj)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\auth\admin.py", line 82, in get_form
return super(UserAdmin, self).get_form(request, obj, **defaults)
File "C:\Users\Andriken\Documents\Prac\djangoexample\bookmarks\mysite_env\lib\site-packages\django\contrib\admin\options.py", line 646, in get_form
% (e, self.__class__.__name__)
django.core.exceptions.FieldError: Unknown field(s) (password2, password1) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.
[07/Jul/2018 21:14:51] "GET /admin/account/customuser/add/ HTTP/1.1" 500 182719
forms.py ---------------------------------------
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from account.models import CustomUser
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
class Meta:
fields = ('email','date_of_birth',)
model= CustomUser
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError('Passwords don\'t match')
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password2'])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = CustomUser
fields = ('email','password','date_of_birth','is_active','is_admin')
def clean_password(self):
return self.initial['password']
admin.py-------------
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from account.forms import UserCreationForm, UserChangeForm, ExtendedUserCreationForm
from .models import CustomUser
class CustomUserAdmin(BaseUserAdmin):
form = UserCreationForm
add_form = UserChangeForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'date_of_birth', 'is_admin','is_active')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('date_of_birth',)}),
('Permissions', {'fields': ('is_admin','is_active',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('date_of_birth','email','password1','password2')}
),
)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
admin.site.register(CustomUser, CustomUserAdmin)
admin.site.unregister(Group)
Please anyone try to let me know where am I missing something, I can't solve this, I also tried overriding the UserCreationForm for both Password1 and Password2 fields again to see if it works, but no luck. Seems it's the same
In your CustomUserAdmin
try switching form
and add_form
values
Try:
class CustomUserAdmin(BaseUserAdmin):
form = UserChangeForm
add_form = UserCreationForm
add_form
for creation of new users.
form
for editing
I hope this will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With