简体   繁体   中英

Extended User model in django not saving profile image

Am trying to create a chat forum in django . but to do this i needed to extend the User Model, But after extending it the profile image does not save This is my model

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email = models.EmailField()
img = models.FileField(upload_to='media/', blank=True, null=True)

def __str__(self):
    return self.user.username
@receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
    Profile.objects.create(user=instance)
instance.profile.save()

My View:

def signup(request):
if request.method == 'POST':
    form = SignUpForm(request.POST)
    if form.is_valid():
        user = form.save()
        user.refresh_from_db()  # load the profile instance created by the signal
        user.profile.email = form.cleaned_data.get('email')
        user.profile.img = form.cleaned_data.get('img')
        user.save()
        raw_password = form.cleaned_data.get('password1')
        user = authenticate(username=user.username, password=raw_password)
        login(request, user)
        return redirect('home')
else:
    form = SignUpForm()
return render(request, 'tforum/signup.html', {'form': form})

My Forms.py

class SignUpForm(UserCreationForm):
email = forms.EmailField(help_text='Required.')
img = forms.FileField(help_text='Upload Image')
class Meta:
    model = User
    fields = ('username', 'email', 'img', 'password1', 'password2', )

Signup.html

 <form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
  <p>
    {{ field.label_tag }}<br>
    {{ field }}
    {% if field.help_text %}
      <small style="color: grey">{{ field.help_text }}</small>
    {% endif %}
    {% for error in field.errors %}
      <p style="color: red">{{ error }}</p>
    {% endfor %}
  </p>
{% endfor %}
<button type="submit">Sign up</button>

settings.py

STATIC_URL = '/static/'

MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

The problem is that the user is not created. If i remove the img from the code the user saves.

I have stripped away most of the styling

you should create a profileForm

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('email', 'img')

and update your signup.py

    def update_signup(request):
      if request.method == 'POST':
        form = SignUpForm(request.POST)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)
        if form.is_valid() and profile_form.is_valid():
            user = form.save()
            user.refresh_from_db()  # load the profile instance created by the signal
            user.profile.email = form.cleaned_data.get('email')
            user.profile.img = form.cleaned_data.get('img')
            profile_form.save()
            user.save()
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'tforum/signup.html', {'form': form})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM