我是 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/ em> 或者事实是
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 simple 服务器,则不必关心它) .
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/
关于python - Django 1.8 : Help making filenames RELATIVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761828/