Hacked Existence 可能已将他的 Web 服务器(基于您的上一个问题的 apache)配置为提供静态文件。
user1658078 是正确的,因为您需要以某种方式 serve the static files,并且在开发环境中您可以使用 django 的内置视图 django.contrib.staticfiles.views.serve(request, path) - 这个视图所做的只是查看在 STATICFILES_DIRS 中设置的路径以及每个应用程序内的static 子目录(例如,如果您的项目是我称为mysite 的应用程序,并且它有一个名为blog 的应用程序,那么它将查找mysite/blog/static/),并提供与该部分匹配的任何文件settings.STATIC_URL 值后面的 URL。
最后,值得指出的是,您的设置目前根本不起作用,因为您的 MEDIA_ROOT 和 STATIC_ROOT 目录设置为从 /he/sites/video1.hackedexistence.com/htdocs/ 的子目录提供服务,除非您创建了此目录,否则它将获胜根本不工作。
要修复静态文件、模板、管理文件和上传的文件,请按照以下说明操作:
在您的settings.py 中,将问题中的行替换为以下内容:
import os
PROJECT_DIR = os.path.dirname(__file__)
INSTALLED_APPS = (
'django.contrib.staticfiles',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
# 'django.contrib.admindocs',
'beer', # note - I'm guessing the name of your application is beer
)
# django.contrib.staticfiles app collects files here when we run the collectstatic command
# (depending on your web server config, you may want to change this to e.g. '/var/www/static/' when it comes to deployment)
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static-serve').replace('\\', '/')
# this should be prepended to your urls for static resources
STATIC_URL = '/static/'
# you can put static files which apply to your entire project here
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static").replace('\\', '/'),
)
# the URL to where we have the admin media (static files) from a web browser's perspective (not needed if using Django 1.4 or greater)
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
# should be different from static files dir - this is where uploaded stuff goes
# (depending on your web server config, you may want to change this to e.g. '/var/www/media/' when it comes to deployment)
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media').replace('\\', '/')
# if you need to serve the uploaded stuff again, you need to prefix your urls with this
MEDIA_URL = '/media/'
# you can put templates which apply to your entire project here
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, "templates").replace('\\', '/'),
)
还要确保将DEBUG 设置为True。
在 django 项目中的 urls.py 中(即 not 在 beer 的应用程序目录中),在末尾添加以下内容:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# serving of uploaded media + static files while debug mode is on
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # uploaded media
urlpatterns += staticfiles_urlpatterns() # files in each app's static/ dir
在您的 django 项目目录中创建以下目录:
-
/media/ - 这是files uploaded by users 去的地方(例如,通过FileField 或ImageField)
-
/static/ - 您可以在这里放置适用于整个 django 项目的静态文件(例如,您的整个页面的样式)。例如,使用此配置,如果您尝试访问位于 url static/css/video1.css 的 css 文件 video1.css,则确保该文件位于以下路径:/static/css/video1.css
-
/templates/ - 这是您放置适用于整个 django 项目的模板的地方
-
/beer/static/ - 这是您放置仅适用于一个特定站点的静态文件的地方。在此处构建文件的 url 时,您将它们视为在 /static/ 目录中,因此只需将 STATIC_URL 的值添加到相对文件名中。
-
/beer/templates/ - 当你需要开始为你的视图创建模板时,你把你的模板放在这里(TEMPLATE_LOADERS 设置默认包括django.template.loaders.app_directories.Loader,它会在这个目录中找到模板)。与应用程序特定的静态目录类似,只需将此目录中的文件视为普通/templates/ 目录中的文件即可。
-
/static-serve/ - 这在开发过程中不使用(所以现在不要太担心),但是当你最终想要部署你的 django 应用程序时,你运行 ./manage.py collectstatic,这将导致 django 复制每个文件中的所有文件应用程序目录的static 目录并将它们放在这里,然后您配置您的网络服务器以在 url 以 STATIC_URL 开头的值(在本例中为/static/)时从该目录提供文件。
现在您的静态文件将正确加载,管理员将正确显示,您的用户上传的文件将能够正确提供,并且您的模板将在您需要时正确找到。