在django 1.4开发中使用css [英] use css in django 1.4 development

查看:148
本文介绍了在django 1.4开发中使用css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在django中使用css文件的方法。我尝试了许多事情,我在许多论坛上搜索,但我找不到我的错误。这是我的代码:

I'm looking for a way to use a css file in django. I tried many things and I searched in many forums but I can't find my mistake. This is my code:

目录结构:

/projecte
    manage.py
    /projecte
         settings.py
         url.py
         wsgi.py
         /static
             /css
                  estil.css
         /templates
             inici.html

    /principal
         models.py
         views.py
         forms.py

URL.py

from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
urlpatterns = patterns('',
    url(r'^$','principal.views.inici'),
    ...
)
urlpatterns += staticfiles_urlpatterns()

SETTINGS.py

SETTINGS.py

RUTA_PROJECTE = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(RUTA_PROJECTE,'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (   
os.path.join(RUTA_PROJECTE,'static'),
)

TEMPLATE_CONTEXT_PROCESSORS = ( 
    ...
'django.core.context_processors.static',    
)

INSTALLED_APPS = (    
    'django.contrib.staticfiles',   
    ...
'principal',  
) 

VIEWS.py

def inici(request):
tracks = Track.objects.all()
#print(len(tracks))

return render_to_response('inici.html',
    {'tracks':tracks},
    context_instance = RequestContext(request))

INICI.html

INICI.html

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/estil.css">

当我输入样式不起作用。我没有错,但没有发现。
感谢您的帮助

when I enter the style does not work. I have no error but nothing happends. Thanks you for your help

推荐答案

我认为load staticfiles是一样的。

I think the load staticfiles does the same.

问题是你把它放在.py文件中?它应该在一个模板中。
(或者这似乎是一个打字错误)

The problem is you are putting it in a .py file? It should be in a template. (or that seems to be a typing error)

我看到,你错过了模板的路径:

Ah I see, you miss the path to the template:

return render_to_response('inici.html'

应该是:

return render_to_response('/principal/inici.html'

在主体dir

中创建一个路径模板,并再次称为principal的模板中的子路径:

and a subpath in templates called principal again:

principal/templates/principal

并将你的模板(.html)放在那里

and put your template (.html) there

/projecte
    manage.py
    /projecte
         settings.py
         url.py
         wsgi.py
         /templates (dir for your project templates)
               /projecte
               /admin (e.g. if you want to override your admin templates, do it here)
    /var ( a dir used operationaly for all variable things... logs, files, db)
       /static
          /css
                 estil.css
       /media
       /logs
       /sqlite (for testing only, unless you use sqlite operational as well)
    /principal
         models.py
         views.py
         forms.py
         /templates (a dir for your application templates)
                   /principal
                             inici.html
         /media

,并确保他找到模板...一秒钟:

and make sure he finds the templates... one second:

在settings.py中使用:

use in settings.py:

import os

SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))
BUILDOUT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '..'))
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'static')
STATIC_URL = '/static_media/'
MEDIA_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'media')
MEDIA_URL = '/media/'

TEMPLATE_DIRS = (
    os.path.join(SETTINGS_DIR, "templates"),
)
TEMPLATE_CONTEXT_PROCESSORS = (
 .....
    # Extra for django-staticfiles.
    'staticfiles.context_processors.static_url'
......
)

INSTALLED_APPS (
.....
    'django.contrib.staticfiles',   
    'yourProjectName',
    'yourAppName',    
    'staticfiles',
.....

我会把这样的东西放在你的remplate(.html)文件中:

I would put something like this in your remplate (.html) file:

<link rel="stylesheet"
      href="{{ STATIC_URL }}iamapps/iamapps.css"
      type="text/css"
      media="screen, projection" />

你的模板目录依赖的方式.. ..在您的应用程序路径,如果它属于您的应用程序(主体我猜),在您的网站目录,如果它用于整个网站(projecte)

By the way where you put your templates dir depends.... in your app path if it belongs to your app (principal i guess), in your site dir if it's use to the whole site (projecte)

在您的视图.py有两种方式可以做到,
是古典的方式:(例如)

and in your views .py there are 2 ways to do it, the classical way: (e.g.)

def home(request, template='principal/home.html'):

    logger.debug("Home view called by user %s" % request.user)
    return render_to_response(template,{},context_instance=RequestContext(request))

或基于类的视图:

class HomeView(TemplateView):
    template_name = "principal/home.html"

如果你想做的很好,在你的应用程序模板目录中制作一个基本模板,并扩展这个在您的inici.html中使用:

If you wanna do it really nice, make a base template in your app template dir and extend this one, using in your inici.html:

{% extends 'prinicpal/base.html' %}

在这里编写完整的教程之前,这一切都取决于你定义的内容以及你放在哪里。您的问题不包含所有的信息,以确切指出代码中的错误。

well before writing a complete tutorial here... it all depends on what you define and where you put it. Your question does not contain all the info to point out exactly where the error in your code is.

编辑
随着您的信息补充说,我重建你的项目,它适用于我这个设置:
与您在问题中给出的相同的项目结构。

EDIT With your information added, I rebuild your project, and it works for me with this settings: with the same project structure as you gave in your question.

import os

RUTA_PROJECTE = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(RUTA_PROJECTE,'static')
STATIC_URL = '/static/'
BUILDOUT_DIR = os.path.abspath(os.path.join(RUTA_PROJECTE, '..'))
DEBUG = False


TEMPLATE_DIRS = (
    os.path.join(RUTA_PROJECTE, "templates"),
)


TEMPLATE_CONTEXT_PROCESSORS = ( 
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    # Extra for django-staticfiles.
    'staticfiles.context_processors.static_url',

)


INSTALLED_APPS = (    
    'django.contrib.staticfiles',   
    'principal',
    'projecte',    
    'staticfiles',
) 

ROOT_URLCONF = 'projecte.urls'

编辑(31-12-2012)

EDIT (31-12-2012)

,不要忘记添加项目urls: p>

and do not forget to add in your project urls:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'', include('staticfiles.urls')),
    )

这篇关于在django 1.4开发中使用css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆