如何使我的CSS文件在Django中工作 [英] How to make my css files to work in django

查看:63
本文介绍了如何使我的CSS文件在Django中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是django的新手,但是在python中有一些相关技能。我刚刚开始在Django中构建一个项目,到目前为止,我已经能够在我的项目中创建一个应用程序。另外,我已经能够将我网站的index.html文件链接到django,并且在命令提示符下运行服务器后可以成功查看它。但这是我的问题;

I am new to django but have some relative skills in python. I just started building a project in django and so far I have been able to create an app within my project. Also, I have been able to link the index.html file of my website to django and can successfully view it after running the server on the command prompt. But here are my questions;


  1. 我如何制作CSS,img和js文件可以与我的index.html一起使用?

感谢你们到目前为止的答复,但仍然是index.html页在本地主机地址上加载,但不应用img,css和js脚本。它仅加载html。尽管我尝试使用第二个人对我的帖子进行回复的方法,但仍然没有运气。

Thanks to you guys for the responses so far, but still the index.html page loads on the localhost address but it doesn't apply the img,css and js scripts. It only loads the html. Although i have tried to use the method stated by the 2nd person that responded to my post but still no luck.

推荐答案

用于开发服务器的非常基本的css / img用法的小型演示: python manage.py runserver 。您不应将其用于生产。
对于用户输入/输出,您应该看一下教程

Just a small demo for very basic css/img usage with the development-server: python manage.py runserver. You should not use this for production. For the user input/output you should have a look at the tutorial.

文件结构

project
  |- static
  |   |- css
  |   |   |- stuff.css
  |- media
  |   |- images
  |   |   |- love.jpg
  |- templates
  |- urls.py
  |- __init__.py
  |- manage.py
  |- settings.py

base.html

<html>
<head>
   <link href="{{ STATIC_URL }}css/stuff.css" />
</head>
<body>
   <h1>I like django!</h1>
   <img src="{{ MEDIA_URL }}images/love.jpg" />
</body>
</html>

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
)

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

urls.py

# example windows
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': 'C:/python25/lib/site-packages/project/media/', 'show_indexes': True}),
# example *ix
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': '/usr/lib/python25/site-packages/project/static/', 'show_indexes': True}),

这篇关于如何使我的CSS文件在Django中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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