我的实时网站(Django 1.3)上的所有内容都设置了样式。以下是settings.py的非样板代码:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = "/home2/django/build/projects/static/media/"
MEDIA_URL = "/media/"
STATIC_ROOT = "home2/django/build/projects/static/"
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
这是urls.py的样子:
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'seeking_alpha.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/', 'seeking_alpha.views.about', name = 'about'),
url(r'^guidelines/', 'seeking_alpha.views.guidelines', name = 'guidelines'),
url(r'^participation/', 'seeking_alpha.views.participation', name = 'participation'),
url(r'^evaluation/', 'seeking_alpha.views.evaluation', name = 'evaluation'),
url(r'^home/', 'seeking_alpha.views.home', name = 'home')
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
和views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
def home(request):
c = {}
return render(request, "home.html", c)
def about(request):
c = {}
return render(request, "about.html", c)
def guidelines(request):
c = {}
return render(request, "guidelines.html", c)
def participation(request):
c = {}
return render(request, "participation.html", c)
def evaluation(request):
c = {}
return render(request, "evaluation.html", c)
最后是模板中的一些代码:
{% load static %}
<DOCTYPE! html>
<html>
<head>
<title>2015 UChicago Seeking Alpha Pitch Competition</title>
<link rel = "stylesheet" type = "style/css" href = "{% get_static_prefix %} participation.css">
</head>
<body>
<div class = "container">
<div class = "header">
<img id = "alpha" src = "{% get_static_prefix %} alpha.jpg" height = "60", width = "250">
由Hostgator主持的Django编写。所有的ht逻辑工作正常(链接,内容),没有任何css页面或图像进入用户看到的内容
答案 0 :(得分:0)
尝试在您/
的{{1}}路径的开头插入STATIC_ROOT
。它应该有所帮助,因为现在它是一条相对路径,你需要一个绝对路径。它应该是这样的:
settings.py