profile shape not partially saved

Viewed 17

After the first registration, it transfers to a new form - you can enter a profile email, photo, bio, links to twitter, github and other social networks, 2 models participate: ProfileUser and User. When a person wrote what he wanted and pressed the button, after the transition he clicked on the user and then he was thrown to the profile page, but there, apart from the mail, nothing from the previously entered is shown, when trying to change the profile, nothing is shown either, there is nothing in the form, I went in the admin panel in the model, only the user is also set automatically when switching to creating a profile after registration, other data is simply not entered, please tell me how to fix it

The user appears to me because this is in the registration

profile = ProfileUser.objects.create(user=new_user)

I tried to redo it according to this article did not work

models.py

class ProfileUser(models.Model):
   user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
   img = models.ImageField(null=True, blank=True, upload_to='static/images/',  validators=[img__size])
   birthday = models.DateField(blank=True, null=True)
   about = models.TextField(max_length=1000, null=True)

   #? social media
   twitter = models.URLField(null=True, blank=True)
   facebook = models.URLField(null=True, blank=True)
   instagram = models.URLField(null=True, blank=True)
   telegram = models.URLField(null=True, blank=True)
   vk = models.URLField(null=True, blank=True)
   reddit  = models.URLField(null=True, blank=True)
   github = models.URLField(null=True, blank=True)

   def __str__(self):
       return 'Profile for user {}'.format(self.user.username)

forms.py

class UserEditForm(ModelForm):
   class Meta:
       model = User
       fields = ['email']
class ProfileEditForm(ModelForm):
   class Meta:
       model = ProfileUser
       fields = ['birthday', 'img', 'about', 'twitter', 'facebook','instagram', 'telegram','vk','reddit', 'github']

views.py Create Profile:

def createProfile(request):
   UserForm = UserEditForm()
   ProfileForm = ProfileEditForm()

   if request.method == 'POST':
      print(request.POST)
      UserForm = UserEditForm(request.POST)
      ProfileForm = ProfileEditForm(request.POST)
      if ProfileForm.is_valid():
        UserForm.save()
        ProfileForm.save()
        return redirect('base:home')

context = {'UserForm': UserForm,'ProfileForm': ProfileForm}
return render(request, 'base/profileForm.html', context)

Update Profile:

@login_required(login_url='base:login')
def updateProfile(request,pk):
   user = User.objects.get(id=pk)
   UserForm = UserEditForm(instance=user)
   ProfileForm = ProfileEditForm(instance=user)
   if request.method == 'POST':
      ProfileForm = ProfileEditForm(request.POST,instance=user)
      UserForm = UserEditForm(request.POST,instance=user)
      if UserForm.is_valid():
          UserForm.save()
          ProfileForm.save()
          return redirect('base:home')

context = {'user': user,'UserForm': UserForm,'ProfileForm': ProfileForm}
return render(request, 'base/profileForm.html', context)

profileForm.html

{% block content %}
<form class="login__form" method='POST' action="." enctype="multipart/form-data">
   {% csrf_token %}
   {{ProfileForm.media}}
   {{UserForm.as_p}}
   {{ProfileForm.as_p}}
   <input type="submit" class="btn btn-min" value="Сохранить">
</form>
{% endblock content %}
1 Answers

You will have to pass the ProfileUser instance corresponding to the user to the ProfileEditForm in updateProfile.

ProfileForm = ProfileEditForm(instance=user.profileuser)
Related