0

I'm trying to create a user profile for my users with Django. Overall it seems to have mostly worked and I am able to see everything correct in the admin page. On my actual HTML page I correctly see the model fields that I need, however they aren't populated with any data, and the data I put in won't actually save even though it says it does.

views.py

class DemoUserEditView(UpdateView):
    form_class = DemoUserEditForm
    template_name = "user/profile.html"
    view_name = 'account_profile'
    success_url = '/member/'

    def get_object(self):
        return self.request.user

    def form_valid(self, form):
        form.save()
        messages.add_message(self.request, messages.INFO, 'User profile updated')
        return super(DemoUserEditView, self).form_valid(form)


account_profile = login_required(DemoUserEditView.as_view())

models.py.

class UserProfile(models.Model):

    user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
    avatar_url = models.CharField(max_length=256, blank=True, null=True)

    skills = models.CharField(max_length=256, blank=True, null=True)



    avatarpic = models.ImageField(_('avatar photo'),
    blank=True, null=True,
    upload_to=user_directory_path, validators=[validate_img_extension])

    Bio = models.TextField(_('Bio'),
    max_length=200, blank=True, null=True, unique=False)

    EDUCATION_CHOICES = (
    ('0', "Didn't complete High School"),
    ('1', 'High School or GED'),
    ('2', 'Associate Degree'),
    ('3', 'Batchlors Degree'),
    ('4', 'Masters Degree'),
    ('5', 'PhD Degree'),
    ('6', 'Professional Degree'),
    )

    Education = models.CharField(_('Education'),
    max_length=100, blank=True, null=False, 
    choices=EDUCATION_CHOICES, unique=False, 
    help_text="Level of Education")

urls.py

url(r'^accounts/', include('allauth.urls')),
url(r'^accounts/profile/$', 'base.views.account_profile', name='account_profile'),    

I'm trying to use Generic Views, and I thought UpdateView would be the appropriate tag for this. I have read through the Generic View docs as well as several this stackoverflow question, and this one. My big problem is that I'm not getting anymore error messages and it says the form is valid and saving, but it's not and i'm not sure what else to even try.

So how can I populate my modelform with the existing user data?

Community
  • 1
  • 1
RknRobin
  • 391
  • 2
  • 6
  • 21

1 Answers1

0

As written here

UpdateView wants to show the form already filled with values, so it instances self.object before processing the request. This makes the object available in the keywords dictionary under the instance key, which is used by modelforms to initialize the data.

For me it means that:

def get_object(self):
    return self.request.user

does not return User object which is required for the form. Can you pleas check if it is None?

Edit 1: I have made a local test and after overriding get_form form data was loaded into form:

def get_form(self, form_class=None):

    user = self.request.user
    profile = get_user_profile(self.request.user)
    user_initial = {'username': user.username,
                    'first_name': user.first_name,
                    'last_name': user.last_name,
                    'email': user.email,
                    'phone': profile.phone}

    form = ProfileForm(initial=user_initial, instance=profile)
    return form

As you can see in get_form you can create your form and provide initial data as well as instance.

Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
  • The command self.request.user returns the correct user string. I still think this comand is where the error lies though, I'm just not sure what else to return. – RknRobin Dec 12 '16 at 05:11
  • ok, try then to make like that def get_object(self, queryset=None): – Alexander Tyapkov Dec 12 '16 at 07:57
  • Alright now we're getting somewhere. Adding queryset=None did... nothing! This is shown [here] (https://ccbv.co.uk/projects/Django/1.4/django.views.generic.edit/UpdateView/) but this is because queryset=None is default and the queryset needs to be defined within get_object. I've added queryset = UserProfile.objects.all() but it didn't fix the frontend. – RknRobin Dec 14 '16 at 04:11
  • Your new Edit 1 doesn't work for me. What are you defining for the function `get_user_profile()`? – RknRobin Dec 16 '16 at 18:25
  • I'm currently defining my `form_class` from my views. In your Edit 1, your defining a new `form`, however I should be able to do this from my forms.py instead right? Overall your answer is based around the concept of filling the form with the user information, but in theory I could do this from my forms.py right? – RknRobin Dec 16 '16 at 18:48
  • @RknRobin As you correctly noticed it is just example which shows how to create the form and init it with data. get_user_profile is my internal function so don't look at it. – Alexander Tyapkov Dec 16 '16 at 19:25