Django:更新文件
Django: updating file
我正在尝试上传文件,但上传后我无法打开它,因为它显示一条错误消息,指出没有包含该主键的对象。我认为错误应该在网址或路径中(MEDIA_ROOT),但无法找出问题所在。
有什么建议吗?
setings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
models.py
class Foo(models.Model):
file = models.FileField(upload_to="file_folder/", blank = True, null = True)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
--编辑--
我正在使用管理界面,所以没有 Views.py
并且没有显示回溯,只有以下错误:
错误
Request Method: GET
Request URL:http://localhost:8000/admin/db_personal/foo/3/media/file_folder/django.pdf/
foo object with primary key u'3/media/file_folder/django.pdf' does not exist.
MEDIA_URL 需要是绝对路径,如 STATIC_URL:
MEDIA_URL = "/media/"
我正在尝试上传文件,但上传后我无法打开它,因为它显示一条错误消息,指出没有包含该主键的对象。我认为错误应该在网址或路径中(MEDIA_ROOT),但无法找出问题所在。
有什么建议吗?
setings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
models.py
class Foo(models.Model):
file = models.FileField(upload_to="file_folder/", blank = True, null = True)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
--编辑--
我正在使用管理界面,所以没有 Views.py
并且没有显示回溯,只有以下错误:
错误
Request Method: GET
Request URL:http://localhost:8000/admin/db_personal/foo/3/media/file_folder/django.pdf/
foo object with primary key u'3/media/file_folder/django.pdf' does not exist.
MEDIA_URL 需要是绝对路径,如 STATIC_URL:
MEDIA_URL = "/media/"