2017-09-15 74 views
1

现在我试图在我的Django Hello World程序中使用一些基本的CSS。我可以显示“Hello World”,但CSS效果不起作用。目前这是我在我的主要网址:试图在Django Hello World中使用CSS

from django.conf.urls import url, include 
from django.contrib import admin 
from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'',include('firstapp.urls')), 
] 

这就是我在我的应用程序的URL。

from django.conf.urls import url 
from . import views 

urlpatterns = [ 
    url(r'^$', views.index, name='index'), 
] 

这是我的观点文件。

from django.shortcuts import render 
from django.http import HttpResponse 

def index(request): 
    context = {"variable":"Hello World"} 
    return render(request, "base.html", context) 

我base.html文件文件

{% load staticfiles %} 

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Title of the document</title> 
<link rel='stylesheet' href='{% static "css/base.css" %}'/> 
</head> 
<html> 
<p>{{ variable }}</p> 
<script src="{% static "base.js'" %}"></script> 
</body> 
</html> 

而且我base.css文件。

h1 { 
    color: 00FFFF; 
} 

最后,我在设置文件的底部添加了这段代码。

STATIC_URL = '/static/' 

STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, "static"), 
] 

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn") 

理想情况下,页面应该在青色用的“Hello World”显示...不幸的是它只是显示的“Hello World”在正常的黑色文本。有人知道为什么吗?

回答

2

您需要在CSS中定位正确的元素。目前,你只是造型h1,而在你的HTML你有p。你也需要一个有效的十六进制代码(开头的哈希:#)

更新您的CSS来:

p { 
    color: #00FFFF; 
}