Writing my first Django app - 7

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, these files are called "static files".

1. Customize my app's look and feel

First, create a directory called static in the polls directory. Django will look for static files there.

Within the static directory that has just created, create another directory called polls and within that create a file called style.css.

polls/static/polls/style.css

li a {
    color: green;
}

polls/templates/polls/index.html

{% load static %}

<link rel="stylesheet" href="{% static 'polls/style.css' %}">

Start the server (or restart it if it's already running):

py manage.py runserver

Reload http://localhost:8000/polls/ and the question links are green now.

2. Adding a background-image

Create an images subdirectory in the polls/static/polls/ directory. Inside this directory, put an image called background.jpg.

polls/static/polls/style.css

body {
    background: white url("images/background.jpg") no-repeat;
}

Reload http://localhost:8000/polls/ and the background will be loaded in the top left of the screen.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。