Domanda

I need to link a CSS file to a simple hmtl page and it just doesn't work! I have red the Docs and watched like 10 Videos that develop on Linux Systems, but non of them come along with the Config of Windows!

for some reason the version I'm using creates the Tree in this way! (correct me if am wrong, because am really new to 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

I added the url to settings.py file:

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

views.py looks like this :

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

then I did the Collectstatic command, and a Dir under the name (assets) is added to the root Directory of mysite and my index.html looks like this

{& 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>

and I'm getting this Error now :

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' %}">

PLEASE DON'T Direct me to the DOCS! and NOT a Solution with LINUX file System!

THIS is the kindda things that makes me doubt why I chose to be a WebDev!

È stato utile?

Soluzione

{& load static &}

should probably be

{% load static %}

Altri suggerimenti

I faced similar issue in windows so this is what i did to server my static page with my CSS make a folder named media and place all your css and js file there and now do following step

  • 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 make a mapping of that folder as a url

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

  • HTML(in your media folder all CSS and js should be there)

    script src="{{MEDIA_URL}}bootstrap/js/bootstrap.min.js"> script src="{{MEDIA_URL}}dropzone/dropzone.js">

  • views.py

    def char(request): t = get_template('name.html') html = t.render(Context({'MEDIA_URL':'http://www.google.com/site_media/'})) return HttpResponse(html)

this will solve your problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top