장고 계정에 프로필 이미지 삽입이 안됩니다 ㅠㅠ

회원 가입 창에서 image 첨부하고 제출하면 Image 필수 항목입니다. 이런식으로 뜨면서 image가 user에 첨부가 안되네요... 어떻게 해야 회원가입할 때 유저 프로필 사진을 결합시킬 수 있을까요..??

소스 코드들 입니다.

models.py

from django.db import models
from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)

favorites_list = (
    ('백엔드', '백엔드'),
    ('프론트엔드', '프론트엔드'),
    ('Andriod','Andriod'),
    ('iOS','iOS')
)


class UserManager(BaseUserManager):
    def create_user(self, email, name,date_of_birth, favorites_1, favorites_2 , image, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            date_of_birth=date_of_birth,
            favorites_1 = favorites_1,
            favorites_2 = favorites_2,
            name = name,
            image = image,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, date_of_birth, favorites_1, favorites_2, image, password):
        user = self.create_user(
            email,
            password=password,
            date_of_birth=date_of_birth,
            favorites_1 =favorites_1,
            favorites_2 = favorites_2,
            image = image,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email',
        max_length=255,
        unique=True,
    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    favorites_1 = models.CharField(max_length= 200, choices = favorites_list, default='')
    favorites_2 = models.CharField(max_length= 200, choices = favorites_list, default='')
    name = models.CharField(max_length= 200, default='')
    image = models.ImageField(upload_to='media/whatyourweb/media')


    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth','favorites_1','favorites_2','name','image']

    def __str__(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.is_admin

# class Profile(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     image = models.ImageField(blank=True)                

forms.py

from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField

from .models import User


class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(
        label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('email', 'date_of_birth', 'favorites_1', 'favorites_2','name','image')

    def clean_favorites_2(self):
        favorites_1 = self.cleaned_data.get("favorites_1")
        favorites_2 = self.cleaned_data.get("favorites_2")
        if favorites_1 == favorites_2:
            raise forms.ValidationError("서로 다른 관심분야를 선택해주세요.")
        return favorites_2

    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("패스워드가 일치하지 않습니다.")
        return password2

    def save(self, commit=True):
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = User
        fields = ('email', 'password', 'date_of_birth', 'favorites_1', 'favorites_2',
                  'is_active', 'is_admin','name','image')

    def clean_password(self):
        return self.initial["password"]

signup.html

{% extends "base.html" %}
{% block content %}
<div class="container my-3">
    <div class="row my-3">
        <div class="col-4">
            <h4>계정생성</h4>
        </div>
        <div class="col-8 text-right">
            <span>또는 <a href="{% url 'account:login' %}">로그인 하세요.</a></span>
        </div>
    </div>
    <form method="post" class="post-form">
        {% csrf_token %}
        {% include "form_errors.html" %}
        <div class="form-group">
            <label for="email">이메일 주소</label>
            <input type="text" class="form-control" name="email" id="email"
                   value="{{ form.email.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="name">이름</label>
            <input type="text" class="form-control" name="name" id="name"
                   value="{{ form.name.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="password1">비밀번호</label>
            <input type="password" class="form-control" name="password1" id="password1"
                   value="{{ form.password1.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="password2">비밀번호 확인</label>
            <input type="password" class="form-control" name="password2" id="password2"
                   value="{{ form.password2.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <label for="date_of_birth">생일</label>
            <input type="date" name="date_of_birth"
                   value="{{ form.date_of_birth.value|default_if_none:'' }}">
        </div>
        <div class="form-group">
            <h5>자신이 관심이 있는 개발 분야를 2개를 선택해주세요.</h5>
            <label for="favorites_1">관심있는 개발분야 1</label>
            <select name="favorites_1">
                <!-- <label><input type="checkbox" name="favorites[]" value="백엔드">백엔드</label>
                <label><input type="checkbox" name="favorites[]" value="프론트엔드">프론트엔드</label>
                <label><input type="checkbox" name="favorites[]" value="Andriod">Andriod</label>
                <label><input type="checkbox" name="favorites[]" value="iOS">iOS</label> -->
                <option value="백엔드">백엔드</option>
                <option value="프론트엔드">프론트엔드</option>
                <option value="Andriod">Andriod</option>
                <option value="iOS">iOS</option>

              </select>
              <p></p>
              <label for="favorites_2">관심있는 개발분야 2</label>
              <select name="favorites_2">
                <option value="백엔드">백엔드</option>
                <option value="프론트엔드">프론트엔드</option>
                <option value="Andriod">Andriod</option>
                <option value="iOS">iOS</option>

              </select>

              <h5>프로필 사진을 업로드해주세요.</h5>
              <input type="file" name="image">


        </div>
        <button type="submit" class="btn btn-primary">회원가입</button>
    </form>
</div>
{% endblock %}

lgj 474

2022년 7월 28일 12:28 오후

목록으로