1

我的应用程序允许用户创建图片。

在我的图片模块类中,我最近一直在返回图片对象的描述,如果用户没有提供描述。我得到一个错误。

是否可以返回用户为我的图片模块上传的图像的名称?例如 images.jpg

class Picture(models.Model):
    user = models.ForeignKey(User)
    board = models.ForeignKey(Board,blank=False,null=False)
    image = models.FileField(upload_to="images/",blank=True)
    description = models.TextField(blank=True)
    def __unicode__(self):
        return self.image

我的观点.py

 def PictureCreator(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('world:LoginRequest'))

    if request.method == "POST":
        form = PictureForm(request.POST , request.FILES)
        if form.is_valid():
            picture = Picture(user=request.user)
            image = form.cleaned_data['image']
            if image:
                                 picture.image = form.cleaned_data['image']
                         description = form.cleaned_data['description']
            if description:
                                 picture.description = form.cleaned_data['description']

            board = form.cleaned_data['board']
            if board:
                                 picture.board = form.cleaned_data['board']
            picture.save()
    return render(request,'picture.html',{'form':PictureForm()})

感谢你们对我的帮助

我改变了这个

        image = request.FILES['image']
        if image:
                            picture.image = request.FILES['image']
                    description = form.cleaned_data['description']
4

1 回答 1

1
image = request.FILES.get('image') //return image name
于 2013-03-13T14:02:27.277 回答