关于网友提出的“ 关于Pythonanywhere的静态文件路径的配置”问题疑问,本网通过在网上对“ 关于Pythonanywhere的静态文件路径的配置”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 关于Pythonanywhere的静态文件路径的配置描述:
@terry_hding 你好,想跟你请教个问题:
最近看了你的关于在pyhtonanywhere上部署Django应用的文章,受益很大,在此表示感谢。虽然最终成功部署上去了,但是不足的是本地静态文件一直都获取不到,这使我的setting.py和主要的引用静态文件的页面base.py(其他都是继承它的),还麻烦能不吝赐教一二。谢谢!
-----setting.py--------- # coding=utf-8 """ Django settings for myzone project. Generated by 'django-admin startproject' using Django 1.8.2. 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.dirname(os.path.dirname(os.path.abspath(__file__))) # 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 = 'jqcje%we0+)!jolnwx=hc4kc!lbry(mn*5-!#gx==pj9$6n0-m' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] ADMINS = ( ('xuyang', '18384205780@163.com'), ) MANAGERS = ADMINS # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'zone', 'haystack', 'threadedcomments', 'django_comments', 'django.contrib.sites', 'bootstrap_toolkit', ) # 设置自定义的评论应用 COMMENTS_APP = 'threadedcomments' # 设置站点编号, 根据自己需求编号 SITE_ID = 1 # 设置 Crispy 的样式主题 RISPY_TEMPLATE_PACK = 'bootstrap3' 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', 'django.middleware.locale.LocaleMiddleware', ) ROOT_URLCONF = 'myzone.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, 'myzone/templates'), os.path.join(BASE_DIR, 'zone/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 = 'myzone.wsgi.application' LOGIN_REDIRECT_URL = '/' # 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'), } } # full text search HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'zone.whoosh_cn_backend.WhooshEngine', 'PATH': os.path.join(BASE_DIR, 'whoosh_index'), }, } HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' # Internationalization # https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/ STATC_ROOT = '/home/xuyang007/xuyang-blog/static' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) -----setting.py---------
-----------------------base.html------------------------------ {% load staticfiles %} {% load i18n %}Django Boys Blog -----------------------base.html------------------------------{% if user.is_authenticated %}{% trans 'Hello'%} {{ user.username }}
{% else %} {% endif %}Django Boys Blog
Main Content Section
{% block content %} {% endblock %}Sidebar
      
解决方案1:
你用的是模板加静态文件,我是直接link引用的,所以
你可以试把这句
替换成
并去掉{% load staticfiles %},引用时直接/static/路径,这个是相对路径,已经在pythonanywhere->web->static file映射过的,所以找的到,而用模板加载由django自动生成路径,还不知道具体的路径是什么样,并且能否把你应用的目录树贴一下,方便查找问题,我的目录树是:
myblog/
|-- blog
| |-- __init__.py
| |-- __init__.pyc
| |-- admin.py
| |-- admin.pyc
| |-- migrations
| |-- models.py
| |-- models.pyc
| |-- static
| |-- templates
| |-- tests.py
| |-- urls.py
| |-- urls.pyc
| |-- views.py
| `-- views.pyc
|-- log.txt
|-- manage.py
`-- myblog
|-- __init__.py
|-- __init__.pyc
|-- settings.py
|-- settings.pyc
|-- urls.py
|-- urls.pyc
|-- wsgi.py
`-- wsgi.pyc
希望能帮到你,谢谢