Joe
Joe

Reputation: 239

django: image data not able to be save to db from form

I'm trying to fix this bug couple of days already. I extended User to have one more Image field, however, the image is not saving into the /media/profile path by itself. If I add an image from the admin panel the image will be shown in the front/user (profile image) as well it will be created into the wanted path /media/profile. I'm assuming my code is wrong where I'm creating user and additionally adding path since if I don't add the path it will look at the root media folder for the image.

custom_user = Profile.objects.create(user = user, profile_image= 'profiles/'+today_path+'/'+profile_image)

This is my first Django project its almost done, just this bug I'm not able to fix it. I will appreciate any help on how to fix it. Thank you, thank you, thank you!

register form:

 <form action="{% url 'register' %}" method = 'POST'>
                    {% csrf_token %} 
                    <div class="form-group">
                    <label class = 'fntmk color' for="first_name">Name</label>
                    <input type="text" name="first_name" required>
                  </div>
                  <div class="form-group">
                    <label class = 'fntmk color' for="last_name">Surname</label>
                    <input type="text" name="last_name" required>
                  </div>
                  <div class="form-group">
                    <label class = 'fntmk color' for="username">Username</label>
                    <input type="text" name="username"required>
                  </div>
                  <div class="form-group">
                    <label class = 'fntmk color' for="email">Email</label>
                    <input type="email" name="email"  required>
                  </div>
                  <div class="form-group">
                    <label class = 'fntmk color' for="password">Password</label>
                    <input type="password" name="password"required>
                  </div>
                  <div class="form-group">
                    <label class = 'fntmk color' for="password2">Confirm password</label>
                    <input type="password" name="password2"  required>
                  </div>
                  <div class="form-group">
                    <label class ='fntmk color' for = 'profile_image'>Profile image</label>
                    <strong><input type ='file' name = 'profile_image' id = 'profile_image'></strong>
                  </div>

                  <input type="submit" value="Register" class="btn btn-register btn-block fntmk my2">
              </form>

views>register def>

def register(request):
    if request.method == "POST":
        # Get form values
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
        password2 = request.POST['password2']
        profile_image = request.POST['profile_image']
        print(type(profile_image))
        if len(password) > 6:
            # Check if passwords match
            if password == password2:
                # Check username
                if User.objects.filter(username=username).exists():
                    messages.error(request, 'Username exists')
                    return redirect('register')
                else:
                    if User.objects.filter(email=email).exists():
                        messages.error(request, "Error, email exists")
                        return redirect('register')  
                    else:
                        # if ok
                        user = User.objects.create_user(username=username, password=password, email=email, first_name = first_name, last_name = last_name)
                        todayT = datetime.now()
                        today_path = todayT.strftime("%Y/%m/%d")
                        print(type(today_path))
                        custom_user = Profile.objects.create(user = user, profile_image= 'profiles/'+today_path+'/'+profile_image)
                        print(type(custom_user))
                    # Login after register
                    auth.login(request, user)
                    messages.success(request, "Welcome!" )
                    custom_user.save()
                    return redirect('dashboard')
        else:
            messages.error(request, "Wrong password!" ) 
            return render(request, 'pages/register.html')
    else:
        return render(request, 'pages/register.html')

models.py>extending User>

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_image = models.ImageField(upload_to = 'profiles/%Y/%m/%d/', blank = True, null = True)

Upvotes: 0

Views: 74

Answers (1)

Shreyash mishra
Shreyash mishra

Reputation: 790

in your views.py use this

 profile_image = request.Files['profile_image']

and in html

<form action="{% url 'register' %}" method'POST' enctype="multipart/form-data">

instead of

 profile_image = request.POST['profile_image']

and tell me if it works

Upvotes: 1

Related Questions