django中的扩展用户模型不保存配置文件图像

时间:2017-10-22 12:11:25

标签: python django

我试图在django中创建一个聊天论坛。但要做到这一点,我需要扩展用户模型,但在扩展后,配置文件图像不会保存 这是我的模特

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()

我的观点:

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})

我的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 =&#39; / static /&#39;

MEDIA_URL =&#39; / media /&#39; MEDIA_ROOT = os.path.join(BASE_DIR,&#39; media&#39;)

问题是没有创建用户。 如果我从用户保存的代码中删除img。

我已经剥离了大部分造型

1 个答案:

答案 0 :(得分:1)

你应该创建一个profileForm

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

并更新您的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})