即使对于管理页面,静态文件也无法在生产环境中使用。
我还没有添加任何静态文件。
我的管理页面样式有问题。
我已经按照以下教程创建了django应用程序。
https://tutorial.djangogirls.org/en/
以下是我的设置。py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '##############################'
# SECURITY WARNING: don't run with debug turned on in production! DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', ]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
ROOT_URLCONF = 'new_p.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'new_p.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
} }
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
}, ]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
我已经在生产中运行了collectstatic。
(master)$ python3 manage.py collectstatic
You have requested to collect static files at the destination location as specified in your settings:
/home/ag/ag.pythonanywhere.com/new_p/static
This will overwrite existing files! Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied to '/home/agusm/agusm.pythonanywhere.com/new_p/static', 119 unmodified.
答案 0 :(得分:2)
我认为您的设置和运行方式collectstatic
很好。
如果您托管在 pythonanywhere 上(我假设您基于ALLOWED_HOSTS
,则需要遵循this guide:
设置静态文件映射
最后,设置一个静态文件映射,使我们的Web服务器为您提供静态文件。
转到PythonAnywhere仪表板上的“ Web”选项卡 转到“静态文件”部分 在网址部分输入与STATIC_URL相同的网址(通常为/ static /) 输入从STATIC_ROOT到路径部分的路径(完整路径,包括/ home / username / etc)
然后点击“重新加载”并通过检索已知的静态文件来测试您的静态文件映射。
例如,如果您在/home/myusername/myproject/static/css/base.css中有一个文件,请访问http://www.your-domain.com/static/css/base.css
基本上,这是您通过设置网络服务器(pfitzer's answer中的nginx / apache)来手动执行的操作,但是我认为它执行的操作与通过pythonanywhere的网络界面设置目录别名相同。
答案 1 :(得分:1)
如果您使用nginx,则需要在配置中使用它
server{
...
location /static/ {
alias /path/to/static/;
...
}
...
}
对于这样的阿帕奇
<VirtualHost *:80>
...
Alias /static "/path/to/static/"
<Directory "/path/to/static">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
答案 2 :(得分:0)
答案 3 :(得分:0)
将这些文本粘贴到settings.py文件中
STATIC_URL = '/static/'
STATICFILES_DIRS=[os.path.join(BASE_DIR,"static"), "templates"]
MEDIA_URL="static/media/"
MEDIA_ROOT=os.path.join(BASE_DIR,"static/media/")
然后将{% load static %}
粘贴到要使用静态文件或媒体的html文件中
然后将<img src="{% static 'media/images/mandeep.jpeg' %}" alt="My image">
用于图像
对于CSS链接,请使用<link rel="stylesheet" href="{% static '/css/style.css' %}">
随时问任何问题
答案 4 :(得分:-1)
运行collectstatic时,默认STATICFILES_FINDERS值django.contrib.staticfiles.finders.FileSystemFinder将从您在STATICFILES_DIRS中具有的任何路径收集静态文件。
另一个默认的STATICFILES_FINDERS值django.contrib.staticfiles.finders.AppDirectoriesFinder将在INSTALLED_APPS中任何应用程序的/ static /文件夹中查找。
找到的所有静态文件将放置在指定的STATIC_ROOT目录中。
$ python manage.py collectstatic
这会将所有文件从您的静态文件夹复制到STATIC_ROOT目录中。
您也可以使用
python manage.py findstatic
查看collectstatic将进入哪些目录。