- 论坛徽章:
- 0
|
本帖最后由 hhn1020 于 2015-09-22 14:31 编辑
本人新手一枚,最近尝试着在新浪sae上搭建一个个人博客。现在遇到的问题如下:
在网上查阅很多关于css和js引用的解决方案后,我选择了下面这种:
在urls.py中有如下定义:
urlpatterns = patterns('',
url( r'^static/(?P<path>.*)$', 'django.views.static.serve',{ 'document_root': settings.STATIC_ROOT }),
url(r'^$','Pytest.views.home'),
url(r'^about/','Pytest.views.about'),
)
在settings.py中配置如下:
STATIC_ROOT = ''
STATIC_URL = '/static/'
from os import path
STATICFILES_DIRS = (
path.join(path.dirname(__file__), '../static/').replace('\\','/')
在views中配置如下:
def home(request):
return render_to_response('web/index.html', locals())
def about(request):
return render_to_response('web/about.html',locals())
然后在templates中的网页里,所有需要引用的不管是css/js/image等都是用href="{% static "css/web_style.css" %}"类似于这样的形式(页面还加上了{% load staticfiles %}这一句)。当然这样是没有问题的,只是感觉有点繁琐。
现在的问题是,如果我想不用href="{% static "css/web_style.css" %}",而是直接用href="css/web_style.css"的话,在采用如上代码中的跳转后,如何保证css也能正常加载。可能没有说清楚,举个例子,现在我按照网上提供的方法,通过在urls.py中添加url(r'^css/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATICFILES_DIRS+'/css'}),那么css文件在index.html中采用href="css/web_style.css"是正常加载的。我从index.html中点击about链接,那么就跳转到Pytest.sinaapp.com/about/,这个时候,如果我用href="css/web_style.css"这种形式加载css的话,查看源代码显示的css的路径为Pytest.sinaapp.com/about/css/web_style.css。很显然,这样是找不到css文件,因为css文件的路劲是Pytest.sinaapp.com/css/web_style.css。我想问下这种情况该怎么解决,或者有没有比上面说的那种方法更好的。 |
|