Hi all, please I'm having an issue adding "image upload" to the user registration form

ND: the first name, last name, username, email and password are working fine. The image doesn’t work because I don’t know how to write the code. Thanks you all

Here is my views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.models import User, auth

Create your views here.

def homepage(request):

return render(request,'project1/homepage.html')

def register(request):
if request.method == ‘POST’:
first_name = request.POST[‘first_name’]
last_name = request.POST[‘last_name’]
username = request.POST[‘username’]
password1 = request.POST[‘password1’]
password2 = request.POST[‘password2’]
email = request.POST[‘email’]
image = request.POST[‘image’]

    if password1==password2:`enter code here`
        if User.objects.filter(username=username).exists():
            messages.info(request, 'Username taken')
            return redirect('register')
        elif User.objects.filter(email=email).exists():
            messages.info(request, 'Email taken')
            return redirect('register')
             user = User.objects.create_user(username=username, password= password1,email=email, first_name=first_name, last_name=last_name)
             messages.info(request,'user created')
             return redirect('login')
        messages.info(request,'password not matching...')
        return redirect('register')
    return redirect('register')


    return render(request, 'project1/register.html')

See the docs for File Uploads. For more details, see Uploaded Files and Upload Handlers.

You might also want to review Working with Forms and the Forms API for the more appropriate ways to access the form fields being submitted.

1 Like