I am making a Django project where users can sign up, sign in, view their profile and change it if they want to.I want to know how to implement the functionality of a user changing their profile?Here is my user form in forms.py```class CustomerForm(forms.Form): username = forms.CharField(label='Desired Username', max_length=150, validators=[validate_username]) first_name = forms.CharField(label='First Name', max_length=150) last_name = forms.CharField(label='Last Name', max_length=150) email = forms.EmailField(label='Your Email', validators=[validate_email]) password1 = forms.CharField(label='Enter Password', widget=forms.PasswordInput, min_length=8) password2 = forms.CharField(label='Retype Password', widget=forms.PasswordInput, min_length=8) def clean(self): cleaned_data = super().clean() password1 = cleaned_data.get("password1") password2 = cleaned_data.get("password2") if password1 != password2: raise ValidationError( "Passwords do not match" )```Here is my sign-up method in views.py```"""Function for sign up."""def sign_up(request): if request.method == 'POST': form = CustomerForm(request. POST) if form.is_valid(): try: user = User.objects.create_user( request.POST['username'], request.POST['email'], request.POST['password1'] ) user.first_name = request.POST['first_name'] user.last_name = request.POST['last_name'] user.save() request.session['normal_username'] = request.POST['username'] login(request, user) return redirect('../book/') except Exception: return HttpResponse("Something went wrong. Please try again.") else: context = {'form': form} return render(request, 'sign_up.html', context) context = {'form': CustomerForm()} return render(request, 'sign_up.html', context)```I cannot user UserCreationForm because it is depreciated.The main problem that I am facing is that I cannot populate the form using 'instance' parameter since I am using Form instead of ModelForm. Is there a way to populate the form?```"""Function for editing profile."""def edit_profile(request): profile = User.objects.get(username=request.session['normal_username']) if request.method == 'POST': form = CustomerForm(request.POST, instance=profile) if form.is_valid(): form.save() return redirect('../book/') else: context = {'form': form} return render(request, 'sign_up.html', context) context = {'form': CustomerForm(instance=profile)} return render(request, 'edit.html', context)
2335 visits
Outline:Creating Forms in Django * HTML Forms * Create a Django form * Django inbuilt libraries * Create views to handle form submission * CSRF - Cross Site Request Forgery Protection * GET and POST Request * Form validation * Add Blog via form input method * Display the blog list * Edit existing blog
Creating Forms in Django * HTML Forms * Create a Django form * Django inbuilt libraries * Create views to handle form submission * CSRF - Cross Site Request Forgery Protection * GET and POST Request * Form validation * Add Blog via form input method * Display the blog list * Edit existing blog
Show video info
Pre-requisite