为CSS文件设置Django [英] Setting up django for css file

查看:104
本文介绍了为CSS文件设置Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新手,正在尝试创建个人技术博客.我主持webfaction.我已使用此视频设置了博客.但是样式表不起作用.我遵循了分步过程创建webfaction提及的静态文件,但样式表仍无法正常工作.

I am a newbie to django and trying to create a personal tech blog. I am hosting on webfaction. I have setup the blog using this video. But the stylesheets are not working. I followed the step-by-step procedure to create the static files mentioned by webfaction but still the stlyesheet is not working.

我创建了一个静态应用程序:staticapp,并使用/static将其添加到网站中 如webfaction文档中所述.

I have created a static application: staticapp and added it to the website with /static as mentioned in the webfaction doc.

settings.py中:

  STATIC_ROOT = 'home/myaccount/webapps/staticapp'
  STATIC_URL = 'http://myblogtest.myaccount.webfactional.com/static'
  STATICFILES_DIRS = ( 'junk','static',)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
     'django.contrib.admin',
     'django.contrib.admindocs',
     'blog',
     )

我是从我的html页面引用的,就像这样:

I am referencing from my html page like this:

 <link rel="stylesheet" type="text/css" href="static/style/style.css" />

我错过任何一步了吗?我可以看到很多答案说要在urls.py中添加代码段,但Webfaction的文档中并未提及.所以我还没做到.

Am I missing any step? I could see many answers saying to add a code snippet in urls.py but it is not mentioned in Webfaction's doc. So I haven't done that.

我可以看到很多人都遇到过同样的问题,但是由于我是新手,所以答案并没有帮助我.请帮助我进行设置. :)

I could see many have faced this same problem but the answers are not helping me as I am a newbie. Kindly help me to setup this. :)

推荐答案

STATIC_ROOT需要有一个斜杠/,即/home... not home...

STATIC_ROOT needs to have a prepending slash /, i.e. /home... not home...

请勿static目录添加到STATICFILES_DIRS.您已获我的同意,可以对建议您使用最近的钝器进行打击. static目录在开发中应该根本不存在,而在生产中应该仅通过python manage.py collectstatic创建或修改.

DO NOT add the static directory to STATICFILES_DIRS. You have my permission to smack whomever advised you to do that with the nearest blunt object. The static directory should not exist at all in development, and in production it should only ever be created or modified via python manage.py collectstatic.

DO 将您的静态资源存储在每个应用程序的单独static目录或完全不同的项目级目录中,即,该目录不应与MEDIA_ROOT.然后,将 this 目录添加到STATICFILES_DIRS.

DO store your static resources in either each app's individual static directory or an entirely different project-level directory, i.e. this directory should not be the same as that for MEDIA_ROOT or STATIC_ROOT. Then, add this directory to STATICFILES_DIRS.

在Django 1.4及更高版本中,您应该在模板中使用{% static %}模板标记:

In Django 1.4+ you should use the {% static %} template tag in your templates:

{% load static from staticfiles %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

这将自动为您添加URL的STATIC_URL部分,因此您提供的path参数应该是相对于此的.

This will automatically append the STATIC_URL portion of the URL for you, so the path parameter you provide should be relative to that.

在Django 1.3中,在确保'django.core.context_processors.static'出现在TEMPLATE_CONTEXT_PROCESSORS设置中的某处之后,您将使用{{ STATIC_URL }}.您的视图还需要使用RequestContext,这意味着可以使用基于类的视图,render方法,或者将context_instance=RequestContext(request)传递给render_to_response.

In Django 1.3, you'd use {{ STATIC_URL }} after ensuring that 'django.core.context_processors.static' appears somewhere in your TEMPLATE_CONTEXT_PROCESSORS setting. Your views will also need to utilize RequestContext, which means using either class-based views, the render method, or passing context_instance=RequestContext(request) to render_to_response.

如果您想让Django在开发中轻松提供静态文件,则可以在urls.py的末尾添加以下内容:

If you want Django to serve static files in development for ease, you can add the following to the end of your urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

这在生产环境(DEBUG=False)中将起作用.对于生产,您需要运行python manage.py collectstatic,然后让前端Web服务器(nginx,Apache等)为STATIC_URL提供目录.

This will not work in a production enviroment (DEBUG=False). For production, you need to run python manage.py collectstatic and then have your frontend webserver (nginx, Apache, etc.) serve the directory at STATIC_URL.

这篇关于为CSS文件设置Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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