无法更改(编辑)在django上传的文件

时间:2015-03-20 17:42:03

标签: python django edit

我的项目包含一个人的个人资料,他们可以将照片上传为个人资料照片。问题是我无法改变这张照片。在编辑部分,我可以编辑所有其他的东西,如姓名,地址等..但不是照片。

我的models.py是:

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

我的forms.py是:

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

我的views.py是:

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

请帮帮我... P.S我可以从管理页面更改照片。

2 个答案:

答案 0 :(得分:2)

要管理文件,您需要将request.FILES传递给您的表单:

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

有关表单中文件管理的更多说明:https://docs.djangoproject.com/en/1.7/topics/http/file-uploads/

答案 1 :(得分:1)

只需编辑以下代码:

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