我是 Django 的新手。首先,我将解释我的问题和我的逻辑。我想让我的文件路径相对于它们现在的绝对路径,这样我就可以在我的笔记本电脑和 PC 上工作,并让所有内容按原样显示。
我知道我必须在下面的 settings.py 中更改 MEDIA_URL。做
MEDIA_URL = os.path.join(BASE_DIR, 'articles/static/articles/media/')
STATIC_URL = os.path.join(BASE_DIR, 'articles/static/')
有意义吗?从逻辑上讲,我的意思是它从 BASE_DIR 打印到 C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite 的说法会进入 articles/static/articles/media/ 或事实是
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
abspath(...) 改变了一切吗?
settings.py
"""
Django settings for articleTestSite project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
MEDIA_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/static/articles/'
MEDIA_URL = '/media/'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2u@9=qkari39(465g+u!2t7*9tt_pdv)%155jdgxnki5#jujje'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'articles',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'articleTestSite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'articleTestSite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC-5'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_ROOT = 'C:/Users/kevIN3D/Documents/GitHub/articleTestProject/articleTestSite/articles/'
STATIC_URL = '/static/'
urls.py
urlpatterns = [
url(r'^articles/', include('articles.urls')),
url(r'^$', HomePage.as_view(), name='home'),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_ROOT)
我的问题是,当我尝试在声明中使用“/”时,它会将文件名打印为“C:\Users\kevIN3D\Documents\GitHub\articleTestProject\articleTestSite\articles/static/”,因此它使用了不正确的斜杠我自己。
如何使我的页面具有相对文件名?我希望能够在我的笔记本电脑和我的 PC 之间处理这个,但就目前而言,我只能在我的 PC 上处理它,因为所有文件名都是绝对的,只是在我的笔记本电脑上给我断开的链接。
这是存储库的链接 Github Repository .请提供任何帮助,我将不胜感激,我完全被难住了。如果有人可以使用图像和自定义 CSS 来实现这一点,然后让我了解一下您所做的事情。这是我第一次尝试将 Django 文件分发到本地主机。
请您参考如下方法:
让我解释一下设置:
右挥手分隔路径
将每个文件夹传递给 os.path.join() 方法。示例:
os.path.join(BASE_DIR, 'articles', 'static')
STATIC_URL, MEDIA_URL
这个变量被传递给你的模板上下文处理器(如果你使用django.template.context_processors.media)没有任何改变。此变量用于创建指向您的静态和媒体内容的客户端链接。
您应该手动设置您的 STATIC_URL 和 MEDIA_URL。像那样:
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
然后你可以在模板中使用它们:
<img src="{{STATIC_URL}}img/logo.png">(...)
STATIC_ROOT、MEDIA_ROOT、STATICFILES_DIRS
STATIC_ROOT 用于通过 collectstatic 命令将所有静态文件放在一个地方(如果您使用的是 django 简单服务器,则不必关心它) .
STATICFILES_DIRS 是您的 django 简单服务器(manage.py runserver 命令)获取要提供的文件时的文件夹。如果您将运行 collectstatic 命令,则将 STATICFILES_DIRS 中列出的每个目录中的每个文件复制到 STATIC_ROOT 中。
MEDIA_ROOT 是 Django 保存用户上传文件的文件夹(ImageField、FileField)
您应该在 STATIC_ROOT、MEDIA_ROOT、STATICFILES_DIRS 中使用绝对路径:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'example', 'static', 'folder',)
请注意。如果您的应用程序文件夹中有 static 文件夹,并且您的应用程序列在 INSTALLED_APPS 中,此文件夹将自动添加到您的 STATICFILES_DIRS。
在开发期间提供静态和媒体文件
这真的很简单,只需在您的 urlpatterns 之后添加以下行:
urlpatterns = patterns('',
... your patterns...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
不要忘记包含设置:
from django.conf import settings
在生产服务器上服务
运行
collectstatic命令将所有静态内容集中在一个地方。使用您的 apache/nginx/etc 服务器为您的静态和媒体文件夹提供服务:
别名/media//path/to/your/media 别名/static//path/to/your/static/
