【问题标题】:How to server Static Files with Django 1.4 on Windows?如何在 Windows 上使用 Django 1.4 服务静态文件?
【发布时间】:2013-04-27 08:03:54
【问题描述】:

我需要将一个 CSS 文件链接到一个简单的 hmtl 页面,但它不起作用!我把 Docs 弄红了,看了 10 个在 Linux 系统上开发的视频,但没有一个是随 Windows 的配置一起出现的!

由于某种原因,我正在使用的版本以这种方式创建树! (如果错了,请纠正我,因为我对 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命令,在mysite的根目录下添加了一个名为(assets)的Dir 我的 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 文件系统的解决方案!

这有点让我怀疑我为什么选择成为 WebDev!

【问题讨论】:

    标签: django python-2.7 django-staticfiles web-development-server


    【解决方案1】:
    {& load static &}
    

    应该是

    {% load static %}
    

    【讨论】:

    • 妈!我欠你的学位!这是我的毕业顶点!我差点把头发从头上拔下来!想象一下,我已经制作了一个文本引擎,但这阻止了我设计页面!
    【解决方案2】:

    我在 Windows 中遇到了类似的问题,所以这就是我用我的 CSS 为我的静态页面提供服务所做的事情 创建一个名为 media 的文件夹并将所有 css 和 js 文件放在那里,然后执行以下步骤

    • settings.py MEDIA_ROOT = "".join(os.path.join(os.path.dirname(file), 'media').replace('\','/'))

      STATICFILES_DIRS = ( os.path.join(os.path.dirname(file),'static').replace('\','/'), os.path.join(os.path.dirname(file),'media').replace('\','/'), )

      STATIC_DOC_ROOT = '/path/to/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"> 脚本 src="{{MEDIA_URL}}dropzone/dropzone.js">

    • views.py

      def 字符(请求): t = get_template('name.html') html = t.render(上下文({'MEDIA_URL':'http://www.google.com/site_media/'})) 返回 HttpResponse(html)

    这将解决您的问题。

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 2012-06-17
      • 2013-05-15
      • 2013-02-26
      • 2012-04-19
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多