将样式表链接到Django模板 [英] Link stylesheets to Django template
问题描述
我一直在查看本教程,现在在其中有一个样式表 / static / styles / 。
I have been looking at this tutorial and now have a stylesheet in /static/styles/.
问题是模板没有将其拾取:
The problem is that the template doesn't pick this up:
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/css/stylesheet.css" />
<title>Search</title>
</head>
<body>
...
我的设置文件中是否需要某些内容?我在哪里错了?
Do I need something in my settings file? Where am I going wrong?
我的项目结构是:
project
- manage.py
- project
- static
- templates
- __init__
- etc..
编辑
我的urls.py现在看起来像这样:
EDIT My urls.py now looks like this:
from django.conf.urls import patterns, include, url
from bible import views
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'bible.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^verses/', views.search),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
这是我的view.py文件
And this is my view.py file
from django.http import HttpResponse, Http404
from django.shortcuts import render
from bible.models import TBbe, TBookNames
def search(request):
errors = []
if 'b' in request.GET:
if 'c' in request.GET:
if 'v' in request.GET:
book = request.GET['b']
chapter = request.GET['c']
verse = request.GET['v']
verses = TBbe.objects.filter(b=book, c=chapter, v=verse)
book = TBookNames.objects.filter(id=book)
books = TBookNames.objects.all;
return render(request, 'verses.html', {'verses': verses, 'book': book, 'books': books})
else:
raise Http404()
else:
raise Http404()
else:
books = TBookNames.objects.all;
return render(request, 'search_verses.html', {'books': books})
推荐答案
问题似乎是我在settings.py文件中需要以下代码:
The problem seemed to be that I needed the following code in my settings.py file:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),
)
与引用我的模板文件夹的代码相同。如果将静态文件放在同一级别,它应该可以工作。这也为您提供了这里每个人的建议,导入了静态等。
which is the same as the code to reference my templates folder. If you put the static files at the same level, it should work. This is also providing you have followed the advice of everyone on here, importing static etc.
这篇关于将样式表链接到Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!