django 上传头像的问题

宁宁123韩 发布于 2013/04/26 17:53
阅读 911
收藏 1

我的views.py

def upload(request):
        if request.user.is_authenticated():
                users = User.objects.filter(pk=request.user.id)
                for user in users:
                        if request.method == "POST":
                                form = UploadForm(request.POST, request.FILES,instance=user)
                        #print form
                                if form.is_valid():
                                        data = form.save()
                                #data=Upload(image=request.FILES['image'])
                                        data.save()
                                else:
                                        form = UploadForm(instance=user)

                                return render_to_response('upload.html', { 'form' : form })
        return render_to_response("upload.html",{"user":user},context_instance=RequestContext(request))
存进数据库只能存一条 我哪里写的不对吗? 求大婶  救济下~~


加载中
0
Eric_isbug
Eric_isbug

首先你的bug到底是什么;

然后,form.save()这里真的可以像你这么用?

都没看出来你存了user对象的哪个字段.

0
宁宁123韩
宁宁123韩

引用来自“Eric_isbug”的答案

首先你的bug到底是什么;

然后,form.save()这里真的可以像你这么用?

都没看出来你存了user对象的哪个字段.

class UploadForm(ModelForm):
        class Meta:
                model = Upload
                exclude = ("user")


class Upload(models.Model):
        user        = models.OneToOneField(User)
        image       = models.ImageField('Label', upload_to='static/media/')

这样是不是更清晰点
返回顶部
顶部