Reputation: 117
I am trying to create a form that allows me to update an extended user model, see my views below
def employee(request, pk):
employee= Employee.objects.get(id=pk)
user = User.objects.get(id=pk)
user_form = UserForm(instance=user)
Employee = EmployeeProfileForm(instance=employee)
if 'edit_employee' in request.POST:
if request.method == 'POST':
user_form = UserForm(request.POST,instance=user)
employee_form = EmployeeProfileForm(request.POST,instance=user.employee)
if user_form.is_valid() and employee_form.is_valid():
user = user_form.save()
employee = employee_form.save(commit=False)
employee.user = user
employee.save()
return redirect('/')
return render(request, 'accounts/new_employee_profile.html', {'user_form': user_form,'employee_form':employee_form})
models.py
class UserManager(BaseUserManager):
def create_user(self, email, password=None,is_active=True, is_staff=False, is_admin=False):
if not email:
raise ValueError("Users must have email address")
user_obj = self.model(email = self.normalize_email(email))
if not password:
raise ValueError("Users must have a password")
user_obj.set_password(password)
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self,email,password=None):
user = self.create_user(email, password=password,is_staff=True)
return user
def create_superuser(self, email, password=None):
user = self.create_user(email, password=password, is_staff=True, is_admin=True)
return user
class User(AbstractBaseUser):
email = models.EmailField(max_length=255,unique=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
# email and password are required by default
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.staff
@property
def is_admin(self):
return self.admin
@property
def is_active(self):
return self.active
class Employee(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=200, null=True)
birth_year = models.CharField(max_length=4, null=True)
department = models.CharField(max_length=200, null=True, blank=True)
profession = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.full_name
forms.py
class UserForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = '__all__'
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(RegisterForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class EmployeeProfileForm(ModelForm):
class Meta:
model = Employee
fields = '__all__'
template
<div class="row">
<div class="col-md-12">
<div class="card card-body">
<form action="" method="POST">
{{user_form.as_p|crispy}}
{{employee_form.as_p|crispy }}
<hr>
<input type="submit" name="Edit">
</form>
</div>
</div>
</div>
I am getting an error below based on my views as follows
The view accounts.views.EmployeeProfile didn't return an HttpResponse object. It returned None instead.
I have a template before the accessing the template to update the form as follows
<td><a class="btn btn-sm btn-info" name= "edit_employee" href="{% url 'employee_profile' employee.id %}">Edit</a></td>
How will I have to change the code shown, in order to update or edit my form?
Given that the form needs to be updated and submitted at the same time as it is a extended user model which is the User model and the employee model.
Upvotes: 0
Views: 108
Reputation: 1309
every view must return a HttpResponse
. The error you are getting is self-explanatory.
here you are getting this error because you have not explicitly returned anything ins case of your if statements are false.
if these cases are false, by default None
will be returned, which is not an expected return data type for Django views.
explicitly returning HttpResponse
in case of following if cases will solve the issue
if 'edit_employee' in request.POST:
if request.method == 'POST':
.
.
if user_form.is_valid() and employee_form.is_valid():
Reference: https://docs.djangoproject.com/en/3.0/topics/http/views/#a-simple-view
Upvotes: 1