Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot change(edit) the file uploaded in django

My project contain a profile for a person, in which they can upload their photo as profile picture. The problem is i cannot change this photo. In the edit section i can edit all other things like name,address etc.. but not the photo.

my models.py is:

def get_upload_file_name(instance,filename):
    return "image/%s_%s"%(str(time()).replace('.','_'),filename)

class Customer_Profile(models.Model):
    user = models.OneToOneField(User)
    first_name = models.CharField(_('First Name'), max_length= 30)
    middle_name = models.CharField(_('Middle Name'), max_length= 30,null = True,blank=True)
    last_name = models.CharField(_('Last Name'), max_length= 30)
    photo = models.ImageField(_('Photo'), upload_to=get_upload_file_name, null=True, blank=True,default='image/nophoto.png')

my forms.py is:

class Customer_Prof(forms.ModelForm):
    class Meta:
        model = Customer_Profile
        fields = ('photo','first_name','middle_name','last_name','address','phone_no')

class update_company_prof(forms.ModelForm):
    class Meta:
        model = Company_Profile
        fields =    ('name','logo','address','phone_no','url','cat_software','cat_electronics','cat_ mechanical','cat_civil','cat_other','specialized_in','prev_projects')

my views.py is:

def edit_customer(request, offset):
    if request.method == 'POST':
        customer_edit = update_customer_prof(request.POST, instance=request.user.customer_profile)
        if customer_edit.is_valid():
            customer_edit.save()
            return HttpResponseRedirect('customer/' + offset)
    else:
        customer_edit =  update_customer_prof(instance=request.user.customer_profile)
    return render_to_response('edit_cust_prof.html', {'customer_edit':  customer_edit},
                              context_instance=RequestContext(request))

Please help me... P.S I can change photo from the admin page.

like image 728
Jerin A Mathews Avatar asked Oct 19 '22 15:10

Jerin A Mathews


1 Answers

To manage files, in your views, you need to pass request.FILES to you forms :

customer_edit = update_customer_prof(request.POST, request.FILES, instance=request.user.customer_profile)

For more explanation on file management in forms: https://docs.djangoproject.com/en/1.7/topics/http/file-uploads/

like image 137
Étienne Loks Avatar answered Oct 30 '22 01:10

Étienne Loks