我一直在看this tutorial现在在/static/styles/中有一个样式表。
问题是模板没有选择这个:
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/css/stylesheet.css" />
<title>Search</title>
</head>
<body>
...
我的设置文件中是否需要某些内容?我哪里错了?
我的项目结构是:
project
- manage.py
- project
- static
- templates
- __init__
- etc..
编辑
我的 urls.py 现在看起来像这样:
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 文件
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 文件中需要以下代码:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),
)
这与引用我的模板文件夹的代码相同。如果您将静态文件放在同一级别,它应该可以工作。这也提供了您已遵循此处所有人的建议,导入静态等。
关于django - 将样式表链接到 Django 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237563/