I am trying to give my users ability to upload profile pictures to their account. The problem is I am using Django user model, so I don't have my own model where I can add image = models.ImageField()
This is my forms.py
from django import forms
from django.contrib.auth import authenticate, get_user_model,
class UserRegisterForm(forms.ModelForm):
confirm_email = forms.EmailField()
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput)
image = forms.ImageField(required=False)
class Meta:
model = User
fields = ['email','confirm_email','username','password']
def clean_confirm_email(self):
email = self.cleaned_data.get("email")
confirm_email = self.cleaned_data.get("confirm_email")
if email != confirm_email:
raise forms.ValidationError("Emails do not match")
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError("Email already used")
return email
def clean_confirm_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if password != confirm_password:
raise forms.ValidationError("Passwords do not match")
return password
in my registration page the image button is being displayed, Tho I have no idea how to save the image and link it to the user.
this is my views.py
from django.shortcuts import render, redirect
from django.contrib.auth import (authenticate, get_user_model, login, logout)
from .forms import UserLoginForm, UserRegisterForm
# Create your views here.
def login_view(request):
login_form = UserLoginForm(request.POST or None)
if login_form.is_valid():
username = login_form.cleaned_data.get("username")
password = login_form.cleaned_data.get("password")
user = authenticate(username=username,password=password)
login(request, user)
return redirect('/')
context = {
"login_form": login_form
}
return render(request, 'login.html', context)
def register_view(request):
form = UserRegisterForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
password = form.cleaned_data.get("password")
user.set_password(password)
user.save()
new_user = authenticate(username=user.username,password=password)
login(request, new_user)
return redirect('/')
context = {
'form': form,
}
return render(request, 'signup.html', context)
def logout_view(request):
logout(request)
return redirect('/')
So as u can see I am using Django User Model, so my models.py and admin.py is empty.
Most of the solutions they were about having my own Model.