2đź‘Ť
There’s nothing specific to users here. You have two problems: the first is because of your urls. Your profile view does not end in a slash, so the form’s action of “.” sends it to the parent, “/dashboard”, and it does not go through the processing code at all.
url(r'/dashboard/profile/$'...)
Secondly, the model will not be updated because you have not called the form’s valdation method.
if request.method == 'POST':
form = profileForm(data=request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('somewhere')
Note there is no reason to use the commit=False stuff here; the instance is already the user.
0đź‘Ť
I tried with abstract user and it is working perfectly. First of all I created an app named dashboard. Then overwrite default User model.
Now Update dashboard app – models.py, urls.py, forms.py and views.py
In models.py
class User(AbstractUser):
middle_name = models.CharField(max_length=50, blank=True, null=True)
sdl_service_code = models.CharField(choices=SDL_CHOICES, max_length=5)
account = models.CharField(choices=ACCOUNT_CHOICES, max_length=5)
I made This new User model (in dashboard app) django default User model by updating setting file as below
In settings.py
AUTH_USER_MODEL = 'dashboard.User'
In urls.py
urlpatterns = [
path('users', ListUserView.as_view(), name='users'),
path('users/add/', CreateUserView.as_view(), name='users-add'),
path('users/<int:pk>/', UpdateUserView.as_view(), name='users-edit'),
)
]
In forms.py
class UserForm(forms.ModelForm):
middle_name = forms.CharField(required=False, max_length=50)
password = forms.CharField(widget=forms.PasswordInput())
about_company = form.CharField(required=False, max_length=50)
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
for field in iter(self.fields):
self.fields[field].widget.attrs.update({
'class': 'form-control',
'placeholder': '{}'.format(field).replace("_", ' ').capitalize(),
})
self.fields['email'].widget.attrs['placeholder'] = 'abc@xyz.com'
self.fields['email'].required = True
self.fields['first_name'].required = True
self.fields['last_name'].required = True
self.fields['password'].required = True
if self.instance.pk:
self.fields['username'].required = False
self.fields['username'].widget.attrs['readonly'] = True
self.fields['email'].required = False
self.fields['email'].widget.attrs['readonly'] = True
self.fields['password'].widget.attrs['readonly'] = True
self.fields['password'].required = False
def save(self, *args, **kwargs):
self.date_joined = date.today()
super(UserForm, self).save(*args, **kwargs)
return self
class Meta:
model = User
fields = '__all__'
exclude = ('date_joined',)
In views.py
from dashboard.models import User
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from dashboard.forms import UserForm
class CreateUserView(CreateView):
model = User
template_name = 'dashboard/user_add.html'
form_class = UserForm
def get_success_url(self):
return '/users'
class UpdateUserView(UpdateView):
model = User
form_class = UserForm
template_name = 'dashboard/user_add.html'
success_url = '/users'
class ListUserView(ListView):
model = User
template_name = 'dashboard/user_list.html'
- [Answered ]-Django Channels Group.send not working in python console?
- [Answered ]-How to set 2 rows to become header in a dataframe with originally has a header?
- [Answered ]-How to test a Django view which has transaction.atomic(using=myDb)?