0

我需要將一個CSS文件鏈接到一個簡單的hmtl頁面,但它不起作用!我有紅色的文檔,並觀看了Linux系統上開發的10個視頻,但其中沒有一個與Windows的配置一起出現!如何在Windows上使用Django 1.4來服務靜態文件?

由於某種原因我使用的版本以這種方式創建樹! (如果沒猜錯糾正我,因爲我真的很新的Django)

mysite 
    \blog 
    \static 
     style.css 
    \templates 
     index.html 
    __init__.py 
    models.py 
    tests.py 
    views.py 

    \mysite 
    url.py 
    settings.py 
    wsgi.py 
    manage.py 

我加入了網址settings.py文件:

STATIC_ROOT = '' 
STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    ('assets','blog/static'), 

views.py看起來是這樣的:

from django.http import HttpResponse,Http404,HttpRequest 
from django.shortcuts import render_to_response 
def change_form(request): 
    return render_to_response('index.html') 

然後我做了Collectstatic命令,並且名稱下的Dir(資產)被添加到mysite 的根目錄中,我的index.html看起來像這樣

{& load static &} 
<!DOCTYPE html> 
<html> 
    <head> 
     <!--Meta tags--> 
     <meta charset="utf-8"> 
     <!--Title--> 
     <title>Menu Notification Badges</title> 
     <!--Stylesheets--> 
     <link rel="stylesheet" href="{% static 'assets/css/styles.css' %}"> 
</head> 

,我現在收到此錯誤:

TemplateSyntaxError at /change-form/ 

Invalid block tag: 'static' 

Request Method:  GET 
Request URL: http://localhost:8000/change-form/ 
Django Version:  1.4.5 
Exception Type:  TemplateSyntaxError 
Exception Value:  

Invalid block tag: 'static' 

Exception Location:  C:\Python27\lib\site-packages\django\template\base.py in invalid_block_tag, line 321 
Python Executable: C:\Python27\python.exe 
Python Version:  2.7.3 
Python Path:  

['E:\\DjangoSites\\mysite', 
'C:\\Windows\\system32\\python27.zip', 
'C:\\Python27\\DLLs', 
'C:\\Python27\\lib', 
'C:\\Python27\\lib\\plat-win', 
'C:\\Python27\\lib\\lib-tk', 
'C:\\Python27', 
'C:\\Python27\\lib\\site-packages'] 

Server time: Sat, 27 Apr 2013 09:57:21 +0300 
Error during template rendering 

In template E:\DjangoSites\mysite\blog\templates\index.html, error at line 13 
Invalid block tag: 'static' 
3 <html> 
4 <head> 
5 
6 <!--Meta tags--> 
7 <meta charset="utf-8"> 
8 
9 <!--Title--> 
10 <title>Menu Notification Badges</title> 
11 
12 <!--Stylesheets--> 
13 <link rel="stylesheet" href="{% static 'assets/styles.css' %}"> 

請不要將我的文檔!而不是LINUX文件系統的解決方案!

這是kindda的事情,讓我懷疑爲什麼我選擇了WebDev!

回答

4
{& load static &} 

也許應該

{% load static %} 
+0

MAAAN!我欠你跟我的學位!這是我的畢業頂層!我是如此接近拔頭髮出來的我頭! Ima gine我已經制作了一個文本引擎,但是這阻止了我設計這些頁面! – 0bserver07 2013-04-27 08:18:13

1

我在窗戶面臨着同樣的問題,所以這是我做的服務器我的靜態頁面,我的CSS 使文件夾命名爲媒體和將所有的CSS和JS文件中有,現在就接下來的步驟

  • settings.py MEDIA_ROOT = 「」。加入(os.path.join(os.path.dirname(文件), '媒體')。代替( '\', '/'))

    STATICFILES_DIRS =( os.path.join(os.path.dirname(文件) '靜態')。取代( '\', '/'), os.path.join(os.path.dirname(文件), '媒體')。代替( '\', '/'), )

    STATIC_DOC_ROOT = '/路徑/到/ media'

  • urls.py 使該文件夾的映射爲URL

    (R '^ site_media /(P *)$?', 'django.views.static.serve',{ 'DOCUMENT_ROOT':settings.MEDIA_ROOT}),

  • HTML(在你的媒體文件夾中的所有CSS和js應該在那裏)

    script src =「{{MEDIA_URL}} bootstrap/js/bootstrap.min.js」> script src =「{{MEDIA_URL}} dropzone/dropzone。JS「>

  • views.py

    DEF炭(請求): 噸= get_template( 'name.html') HTML = t.render(上下文({ 'MEDIA_URL': 'http://www.google.com/site_media/'} )) 返回的HttpResponse(HTML)

這將解決你的問題。

相關問題