如何上传图像并将其链接存储在数据库中

发布于 2024-12-11 16:32:56 字数 1131 浏览 0 评论 0原文

我在 Django 网站上阅读了一些文档,例如: 基本文件上传FileField.storage。但是,我仍然不明白如何将文件(或图像)上传到服务器并将其链接存储在数据库中。我想将文件写入以下目录,例如:'image/%Y/%m/%d'

请给我一个示例代码。太感谢了。

我的代码如下:

#models.py
class Image(models.Model):
   imageid = models.AutoField()
   title = models.CharField(max_length=100)
   imagepath = models.ImageField(upload_to='images/%Y/%m/%d/')       

#forms.py
class UploadFileForm(forms.Form):
   title = forms.CharField(max_length=100)
   image = forms.FileField()   

#views.py
def upload_file(request):
   if request.method == 'POST':
      form = UploadFileForm(request.POST, request.FILES)
      if form.is_valid():
         # How to upload file to folder named 'images/%Y/%m/%d/'

         # How to save the link above to database

         return HttpResponseRedirect('/success/url/')
   else:
      form = UploadFileForm()
return render_to_response('upload.html', {'form': form})

I read some documents in Django site such as: Basic file uploads and FileField.storage. However, I still don't understand how to upload a file (or an image) to server and store its link in database. I would like to write the files to the following directory such as: 'image/%Y/%m/%d'

Please give me a sample code. Thank you so much.

My code follows as:

#models.py
class Image(models.Model):
   imageid = models.AutoField()
   title = models.CharField(max_length=100)
   imagepath = models.ImageField(upload_to='images/%Y/%m/%d/')       

#forms.py
class UploadFileForm(forms.Form):
   title = forms.CharField(max_length=100)
   image = forms.FileField()   

#views.py
def upload_file(request):
   if request.method == 'POST':
      form = UploadFileForm(request.POST, request.FILES)
      if form.is_valid():
         # How to upload file to folder named 'images/%Y/%m/%d/'

         # How to save the link above to database

         return HttpResponseRedirect('/success/url/')
   else:
      form = UploadFileForm()
return render_to_response('upload.html', {'form': form})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2024-12-18 16:32:56

我相信,如果您将上传表单创建为模型表单,然后将其保存在视图中,它将具有将文件保存到文件系统和模型路径的效果。这是一个基本的例子,但我认为它应该有效。

# forms.py
class UploadFileForm(forms.ModelForm):
    class Meta:
        model = models.Image

# views.py
...
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
    form.save()
    return HttpResponseRedirect('/success/url/')

I believe if you create the upload form as a model form and then just save it in the view, it will have the effect of saving the file to the filesystem and the path to the model. This is a basic example, but I think it should work.

# forms.py
class UploadFileForm(forms.ModelForm):
    class Meta:
        model = models.Image

# views.py
...
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
    form.save()
    return HttpResponseRedirect('/success/url/')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文