Django文件上传

发布于 2024-09-10 13:59:42 字数 758 浏览 0 评论 0原文

我创建了一个应用程序,它将在特定位置上传文件。模型保存后如何读取上传的文件?当我单击 change_field_page 上的文件链接时,它给出找不到页面。我为此使用 Django 1.2 和 django-admin 。

这是我的 models.py

class UploadClass(models.Model):
  id=models.AutoField(primary_key=True)
  template_name=models.ForeignKey(sas,verbose_name=ugettext_lazy('Template Name'))
  sample=models.FileField(upload_to='%Y/%B/',verbose_name=ugettext_lazy('Sample'))
  status=models.IntegerField(ugettext_lazy('Status'),choices=statusChoices,default=0)
  created_on=models.DateTimeField(ugettext_lazy('Created on'),auto_now_add=True)

  def __unicode__(self):
    return (self.template_name.name)

我没有在 forms.py 中执行任何操作。保存对象后如何打开文件?

一种方法是为“url”创建一个视图并返回文件。还有其他人吗?

I have created a app which will upload the file at a particular location. How can I read the file uploaded after the model is saved? When I click on the file link on change_field_page it gives page not found. I'm using Django 1.2 and django-admin for this.

Here's my models.py:

class UploadClass(models.Model):
  id=models.AutoField(primary_key=True)
  template_name=models.ForeignKey(sas,verbose_name=ugettext_lazy('Template Name'))
  sample=models.FileField(upload_to='%Y/%B/',verbose_name=ugettext_lazy('Sample'))
  status=models.IntegerField(ugettext_lazy('Status'),choices=statusChoices,default=0)
  created_on=models.DateTimeField(ugettext_lazy('Created on'),auto_now_add=True)

  def __unicode__(self):
    return (self.template_name.name)

I'm not doing anything informs.py. How can I open the file after saving the object?

One way to do this is to create a view for the 'url' and return the file. Are there any others?

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

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

发布评论

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

评论(1

迷鸟归林 2024-09-17 13:59:42

如果文件未从管理员正确链接,请检查您的 MEDIA_ROOTMEDIA_URL 是否最终指向同一位置。另外,您能否举例说明 %Y/%B/ 如何作为文件夹名称?他们可能不像你想象的那样。

In terms of the file not being linked correctly from the admin, check your MEDIA_ROOT and your MEDIA_URL point to, ultimately, the same place. Also, can you give examples of how the %Y/%B/ is working out as folder names, please? They may not be as you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文