光阴的故事明月共光辉

Writing my first Django app - 7

2020-08-04  本文已影响0人  游文影月志

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.

上一篇下一篇

猜你喜欢

热点阅读