Django文件上传
我创建了一个应用程序,它将在特定位置上传文件。模型保存后如何读取上传的文件?当我单击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果文件未从管理员正确链接,请检查您的
MEDIA_ROOT
和MEDIA_URL
是否最终指向同一位置。另外,您能否举例说明%Y/%B/
如何作为文件夹名称?他们可能不像你想象的那样。In terms of the file not being linked correctly from the admin, check your
MEDIA_ROOT
and yourMEDIA_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.