我一直在/ home /' list'中得到一个' AttributeError。对象没有属性' resolve''错误。我已经通过多种方式更改了代码并阅读了文档,但我仍然感到困惑。
' python manage.py findstatic /file/css/syle.css'也没有多大帮助。
来自urls.py的:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import patterns, include, url
from django.contrib import admin
from home import views
admin.autodiscover()
urlpatterns = [ patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact"))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
来自settings.py:
STATIC_URL = 'home/static/'
STATIC_ROOT = 'sitename/home/static/css.js'
STATIC_DIRS = 'home/static'
项目结构:
sitename
db.sqlite3
home
__init.py
admin.py
models.py
static
home.html
services.html
contact.html
views.py
manage.py
mysite
__init.py
settings.py
urls.py
wsgi.py
我错过了什么?
另外,我非常喜欢您的推荐参考资料。
来自home.html的:
{% load staticfiles %}
{% block doctype %}<!DOCTYPE HTML>{% endblock %}
{% load i18n %}
<html>
<head>
<title>{% block title %}{% endblock %}{% trans "title" %}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
{% block meta_tags %}{% endblock %}
<noscript>
<link rel="stylesheet" href="css/skel.css" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/style-wide.css" />
</noscript>
{% block stylesheet %}{% endblock %}
<script src="js/jquery.min.js"></script>
<script src="js/jquery.doc.min.js"></script>
<script src="js/doc.min.js"></script>
<script src="js/doc-layers.min.js"></script>
<script src="js/init.js"></script>
{% block js %}{% endblock %}
</head>
<body class="{% block bodyclass %}{% endblock%}">
{% block page %}
<div id="header">{% block header_navigation %}
<h1><a href="LOGO.jpg" id="logo">{% trans "header/name of business" %}<em>content description</em></a></h1>
<nav id="nav">
<ul>
<li class="current"><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'services' %}">Services</a></li>
<li><a href="{% url 'contact' %}">Contact Us</a></li>
</ul>
</nav>
{% endblock %}
</div>
<section class="wrapper style1">
<div class="container">
<div class="row 200%">
<section>
<p>content</p>
<p>content</p>
<p>content</p>
</section>
</div>
</div>
</section>
<div id="footer">
{% block footer %}
<div class="container">
<div class="row">
<section class="3u 6u(narrower) 12u$(mobilep)">
</section>
{% endblock %}
</div>
</div>
<div class="copyright">
<ul class="menu">
<li>© All rights reserved</li><li>Adapted by: <a href="">me</a></li>, Original Design: <a href="">someone else</a></li>
</ul>
</div>
</div>
{% endblock %}
</body>
</html>
答案 0 :(得分:3)
您应该移除patterns()
周围的方括号,并将staticfiles_urlpatterns()添加到urls.py
:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^home/$', views.Home, name="home"),
url(r'^services/$', views.Services, name="services"),
url(r'^contact/$', views.Contact, name="contact")
) + staticfiles_urlpatterns()
将设置更改为:
STATIC_URL = '/static/'
STATIC_ROOT = '/absolute/path/to/static/dir/in/doc_root/'
# For example: /var/www/yoursite.com/static/
应在生产服务器上设置 STATIC_ROOT
。开发服务器忽略此设置。
STATIC_DIRS
。 Django自动使用static
中所有应用程序中的INSTALLED_APPS
目录作为静态文件的来源。
要链接到静态资源,您需要使用{% static %}
模板标记。例如,而不是:
<link rel="stylesheet" href="css/style.css" />
代码应该是这样的:
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
BTW不要将模板放入静态目录。 manage.py collectstatic
将为所有人提供这些模板的源代码。我怀疑你不想要这个: - )