S
S
Sergey Nizhny Novgorod2016-06-21 05:24:50
Django
Sergey Nizhny Novgorod, 2016-06-21 05:24:50

How do I fill out a profile/update a profile when available?

Hello.
The situation is this:
There is a user profile, where there is additional information (Avatar, about myself, what I offer).
Task: to make a profile filling form that would accept fields (not necessarily all), and update them (not necessarily all).

class Profile_Form(forms.Form):
    avatar = forms.ImageField(label='Загрузить Аватар', required=False)
    myself = forms.CharField(widget=forms.Textarea(attrs={'class': 'course_review_form_width'}), label='О себе', required=False)
    myoffer = forms.CharField(widget=forms.Textarea(attrs={'class': 'course_review_form_width'}), label='Что предлагаю', required=False)

def update_profile(request, add_id):
    token = {}
    token.update(csrf(request))
    if request.user.is_authenticated():
        current_user = request.user
        if request.POST:
            # profilepage = Profile.objects.get(id = add_id)
            form = Profile_Form(request.POST or None, request.FILES or None)
            if form.is_valid():
                avatar = form.cleaned_data['avatar']
                myself = form.cleaned_data['myself']
                myoffer = form.cleaned_data['myoffer']
                profile_obj = Profile(user=current_user, avatar=avatar, myself=myself, myoffer = myoffer)
                profile_obj.save()
                return redirect('/profile'+ add_id, token)
            else:
                return redirect('/profile'+ add_id, token)
        else:
            return redirect('/profile'+ add_id, token)
    else:
        return redirect('/profile'+ add_id, token)

Now the form is filling out well. But:
- if there is no image, it gives an error that the file will not be found. Although I would like to indicate that the field is optional.
- Unable to update the field. Although the documentation says that the save() method is automatically determined to save or update.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question