关于Pythonanywhere的静态文件路径的配置

007xy易 发布于 2016/07/22 11:59
阅读 507
收藏 0

@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 %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<title>Django Boys Blog</title>
	<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
    <link rel="stylesheet"  type="text/css"  href="{% static 'css/bootstrap-theme.min.css' %}">
	<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> -->
	<link href="http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
	 <link rel="stylesheet" href="{% static 'css/blog.css' %}">
	<!-- <link type="text/css" rel="stylesheet" href="{% static 'css/shCoreDefault.css' %}"/> -->
</head>
<body id="navigation">
	<div class="container">
		<div class="page-header">
			{% if user.is_authenticated %}
			<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
			<a href="{% url 'post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
			<p class="top-menu top-hellow" style="font-size: 23pt;">{% trans 'Hello'%} {{ user.username }}
            <a href="{% url 'django.contrib.auth.views.logout' %}" class="top-menu">
                <span class="glyphicon glyphicon-log-out"></span></a>
			</p>
			{% else %}
				<a href="{% url 'django.contrib.auth.views.login' %}" class="top-menu">
				<span class="glyphicon glyphicon-log-in"></span></a>
			{% endif %}
			<h1 class="top-title" style="font-size: 43pt;"><a href="/">Django Boys Blog</a></h1>
		</div>

		<nav class="navbar navbar-inverse">
			<!-- Brand and toggle get grouped for better mobile display -->
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-menu" aria-expanded="false">
					<span class="sr-only">Toggle navigation</span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="#">Brand</a>
			</div>
			<div id="navbar-menu" class="collapse navbar-collapse">
				<ul class="nav navbar-nav">
					<li class="active"><a href="#">Home</a></li>
					<li><a href="#">Page One</a></li>
					<li><a href="#">Page Two</a></li>
				</ul>
			</div>
		</nav>




		<div id="content" class="row-fluid">
			<div class="col-md-9">
				<h2>Main Content Section</h2>
					{% block content %}
					{% endblock %}
			</div>

			<div class="col-md-3">
				<h2>Sidebar</h2>

				&nbsp&nbsp
				<!-- searchbox START -->
			<div id="searchbox">
				<form action="{% url 'zone.views.full_search' %}" method="get">
					<div class="content">
						<label>
						<input type="text" class="textfield searchtip" name="q" size="24" value="">
						</label>
						<input type="submit" class="button" value="">
					</div>
				</form>
			</div>

					&nbsp&nbsp&nbsp&nbsp
					<div>
					<ul class="nav nav-tabs nav-stacked">
						<li><a href='#'>Another Link 1</a></li>
						<li><a href='#'>Another Link 2</a></li>
						<li><a href='#'>Another Link 3</a></li>
					</ul>
					</div>


				<div id="meta-2" class="widget widget_meta">
                    <h3>功能</h3>
                    <ul>
                        {% if user.is_superuser %}
                        <li><a href="/xadmin">管理站点</a></li>
                        {% endif %}
                        {% if user.is_authenticated %}
                        <li>
                            <a href="{% url 'django.contrib.auth.views.logout' %}">登出</a>
                        </li>
                        {% else %}
                        <li>
                            <a href="{% url 'django.contrib.auth.views.login' %}">登录</a>
                        </li>
                        {% endif %}
                        <li><a href="#">文章<abbr title="RSS">RSS</abbr></a></li>
                    </ul>
                </div>

			</div>
    </div>
    </div>

		<div class="row">
			<div class="col-md-1"></div>
				<div class="col-md-10">
					<div id="footer" >
						<a id="gotop" href="#" onclick="MGJS.goTop();return false;">回到顶部</a>
				</div>
			</div>
		</div>
	<script type="text/javascript" src="{% static 'js/jquery.min.js'%}"></script>
    <script type="text/javascript" src="{% static 'js/bootstrap.min.js %'}"></script>

</body>
</html>
-----------------------base.html------------------------------



加载中
0
terry_hding
terry_hding

你用的是模板加静态文件,我是直接link引用的,所以

你可以试把这句<link rel="stylesheet" href="{% static 'css/blog.css' %}">

替换成 <link rel="stylesheet" href="/static/css/blog.css">

并去掉{% 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

希望能帮到你,谢谢




007xy易
007xy易
谢谢你的耐心解答,确实是,在pythonanywhere里应该用映射过的路径,修改之后确实找到了相应的静态文件
返回顶部
顶部