Skip to content

Commit 5eeba7b

Browse files
committed
Syntax-highlight all the code listings.
1 parent e8585f2 commit 5eeba7b

File tree

12 files changed

+527
-395
lines changed

12 files changed

+527
-395
lines changed

en/css/README.md

Lines changed: 146 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ It was written by programmers who worked for Twitter and is now developed by vol
1818

1919
To install Bootstrap, you need to add this to your `<head>` in your `.html` file (`blog/templates/blog/post_list.html`):
2020

21-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
22-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
23-
21+
```html
22+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
23+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
24+
```
2425

2526
This doesn't add any files to your project. It just points to files that exist on the internet.
2627
Just go ahead, open your website and refresh the page. Here it is!
@@ -29,12 +30,14 @@ Just go ahead, open your website and refresh the page. Here it is!
2930

3031
Looking nicer already!
3132

33+
3234
## Static files in Django
3335

3436
Another thing you will learn about today is called __static files__. Static files are all your CSS and images -- files that are not dynamic, so their content doesn't depend on request context and will be the same for every user.
3537

3638
CSS is a static file, so in order to customize CSS, we need to first configure static files in Django. You'll only need to do it once. Let's start:
3739

40+
3841
### Configure static files in Django
3942

4043
First, we need to create a directory to store our static files in. Go ahead and create a directory called `static` inside your `djangogirls` directory.
@@ -45,12 +48,15 @@ First, we need to create a directory to store our static files in. Go ahead and
4548

4649
Open up the `mysite/settings.py` file, scroll to the bottom of it and add the following lines:
4750

48-
STATICFILES_DIRS = (
49-
os.path.join(BASE_DIR, "static"),
50-
)
51+
```python
52+
STATICFILES_DIRS = (
53+
os.path.join(BASE_DIR, "static"),
54+
)
55+
```
5156

5257
This way Django will know where to find your static files.
5358

59+
5460
## Your first CSS file!
5561

5662
Let's create a CSS file now, to add your own style to your web-page. Create a new directory called `css` inside your `static` directory. Then create a new file called `blog.css` inside this `css` directory. Ready?
@@ -67,79 +73,95 @@ But let's do at least a little. Maybe we could change the color of our header? T
6773

6874
In your `static/css/blog.css` file you should add the following code:
6975

70-
h1 a {
71-
color: #FCA205;
72-
}
76+
```css
77+
h1 a {
78+
color: #FCA205;
79+
}
80+
```
7381

7482
`h1 a` is a CSS Selector. This means we're applying our styles to any `a` element inside of an `h1` element (e.g. when we have in code something like: `<h1><a href="">link</a></h1>`). In this case, we're telling it to change its color to `#FCA205`, which is orange. Of course, you can put your own color here!
7583

7684
In a CSS file we determine styles for elements in the HTML file. The elements are identified by the element name (i.e. `a`, `h1`, `body`), the attribute `class` or the attribute `id`. Class and id are names you give the element by yourself. Classes define groups of elements, and ids point to specific elements. For example, the following tag may be identified by CSS using the tag name `a`, the class `external_link`, or the id `link_to_wiki_page`:
7785

78-
<a href="http://en.wikipedia.org/wiki/Django" class="external_link" id="link_to_wiki_page">
86+
```html
87+
<a href="http://en.wikipedia.org/wiki/Django" class="external_link" id="link_to_wiki_page">
88+
```
7989

8090
Read about [CSS Selectors in w3schools](http://www.w3schools.com/cssref/css_selectors.asp).
8191

8292
Then, we need to also tell our HTML template that we added some CSS. Open the `blog/templates/blog/post_list.html` file and add this line at the very beginning of it:
8393

84-
{% load staticfiles %}
94+
```html
95+
{% load staticfiles %}
96+
```
8597

8698
We're just loading static files here :). Then, between the `<head>` and `</head>`, after the links to the Bootstrap CSS files (the browser reads the files in the order they're given, so code in our file may override code in Bootstrap files), add this line:
8799

88-
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
100+
```html
101+
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
102+
```
89103

90104
We just told our template where our CSS file is located.
91105

92106
Your file should now look like this:
93107

94-
{% load staticfiles %}
95-
<html>
96-
<head>
97-
<title>Django Girls blog</title>
98-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
99-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
100-
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
101-
</head>
102-
<body>
108+
```html
109+
{% load staticfiles %}
110+
<html>
111+
<head>
112+
<title>Django Girls blog</title>
113+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
114+
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
115+
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
116+
</head>
117+
<body>
118+
<div>
119+
<h1><a href="/">Django Girls Blog</a></h1>
120+
</div>
121+
122+
{% for post in posts %}
103123
<div>
104-
<h1><a href="/">Django Girls Blog</a></h1>
124+
<p>published: {{ post.published_date }}</p>
125+
<h1><a href="">{{ post.title }}</a></h1>
126+
<p>{{ post.text|linebreaks }}</p>
105127
</div>
106-
107-
{% for post in posts %}
108-
<div>
109-
<p>published: {{ post.published_date }}</p>
110-
<h1><a href="">{{ post.title }}</a></h1>
111-
<p>{{ post.text|linebreaks }}</p>
112-
</div>
113-
{% endfor %}
114-
</body>
115-
</html>
128+
{% endfor %}
129+
</body>
130+
</html>
131+
```
116132

117133
OK, save the file and refresh the site!
118134

119135
![Figure 14.2](images/color2.png)
120136

121137
Nice work! Maybe we would also like to give our website a little air and increase the margin on the left side? Let's try this!
122138

123-
body {
124-
padding-left: 15px;
125-
}
139+
```css
140+
body {
141+
padding-left: 15px;
142+
}
143+
```
126144

127145
Add this to your CSS, save the file and see how it works!
128146

129147
![Figure 14.3](images/margin2.png)
130148

131149
Maybe we can customize the font in our header? Paste this into your `<head>` in `blog/templates/blog/post_list.html` file:
132150

133-
<link href="http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
151+
```html
152+
<link href="http://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
153+
```
134154

135155
This line will import a font called *Lobster* from Google Fonts (https://www.google.com/fonts).
136156

137157
Now add the line `font-family: 'Lobster';` in the CSS file `static/css/blog.css` inside the `h1 a` declaration block (the code between the braces `{` and `}`) and refresh the page:
138158

139-
h1 a {
140-
color: #FCA205;
141-
font-family: 'Lobster';
142-
}
159+
```css
160+
h1 a {
161+
color: #FCA205;
162+
font-family: 'Lobster';
163+
}
164+
```
143165

144166
![Figure 14.3](images/font.png)
145167

@@ -150,95 +172,105 @@ As mentioned above, CSS has a concept of classes, which basically allows you to
150172

151173
Go ahead and name some parts of the HTML code. Add a class called `page-header` to your `div` that contains your header, like this:
152174

153-
<div class="page-header">
154-
<h1><a href="/">Django Girls Blog</a></h1>
155-
</div>
175+
```html
176+
<div class="page-header">
177+
<h1><a href="/">Django Girls Blog</a></h1>
178+
</div>
179+
```
156180

157181
And now add a class `post` to your `div` containing a blog post.
158182

159-
<div class="post">
160-
<p>published: {{ post.published_date }}</p>
161-
<h1><a href="">{{ post.title }}</a></h1>
162-
<p>{{ post.text|linebreaks }}</p>
163-
</div>
183+
```html
184+
<div class="post">
185+
<p>published: {{ post.published_date }}</p>
186+
<h1><a href="">{{ post.title }}</a></h1>
187+
<p>{{ post.text|linebreaks }}</p>
188+
</div>
189+
```
164190

165191
We will now add declaration blocks to different selectors. Selectors starting with `.` relate to classes. There are many great tutorials and explanations about CSS on the Web to help you understand the following code. For now, just copy and paste it into your `djangogirls/static/css/blog.css` file:
166192

167-
.page-header {
168-
background-color: #ff9400;
169-
margin-top: 0;
170-
padding: 20px 20px 20px 40px;
171-
}
172-
173-
.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
174-
color: #ffffff;
175-
font-size: 36pt;
176-
text-decoration: none;
177-
}
178-
179-
.content {
180-
margin-left: 40px;
181-
}
182-
183-
h1, h2, h3, h4 {
184-
font-family: 'Lobster', cursive;
185-
}
186-
187-
.date {
188-
float: right;
189-
color: #828282;
190-
}
191-
192-
.save {
193-
float: right;
194-
}
195-
196-
.post-form textarea, .post-form input {
197-
width: 100%;
198-
}
199-
200-
.top-menu, .top-menu:hover, .top-menu:visited {
201-
color: #ffffff;
202-
float: right;
203-
font-size: 26pt;
204-
margin-right: 20px;
205-
}
206-
207-
.post {
208-
margin-bottom: 70px;
209-
}
210-
211-
.post h1 a, .post h1 a:visited {
212-
color: #000000;
213-
}
193+
```css
194+
.page-header {
195+
background-color: #ff9400;
196+
margin-top: 0;
197+
padding: 20px 20px 20px 40px;
198+
}
199+
200+
.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
201+
color: #ffffff;
202+
font-size: 36pt;
203+
text-decoration: none;
204+
}
205+
206+
.content {
207+
margin-left: 40px;
208+
}
209+
210+
h1, h2, h3, h4 {
211+
font-family: 'Lobster', cursive;
212+
}
213+
214+
.date {
215+
float: right;
216+
color: #828282;
217+
}
218+
219+
.save {
220+
float: right;
221+
}
222+
223+
.post-form textarea, .post-form input {
224+
width: 100%;
225+
}
226+
227+
.top-menu, .top-menu:hover, .top-menu:visited {
228+
color: #ffffff;
229+
float: right;
230+
font-size: 26pt;
231+
margin-right: 20px;
232+
}
233+
234+
.post {
235+
margin-bottom: 70px;
236+
}
237+
238+
.post h1 a, .post h1 a:visited {
239+
color: #000000;
240+
}
241+
```
214242

215243
Then surround the HTML code which displays the posts with declarations of classes. Replace this:
216244

217-
{% for post in posts %}
218-
<div class="post">
219-
<p>published: {{ post.published_date }}</p>
220-
<h1><a href="">{{ post.title }}</a></h1>
221-
<p>{{ post.text|linebreaks }}</p>
222-
</div>
223-
{% endfor %}
245+
```html
246+
{% for post in posts %}
247+
<div class="post">
248+
<p>published: {{ post.published_date }}</p>
249+
<h1><a href="">{{ post.title }}</a></h1>
250+
<p>{{ post.text|linebreaks }}</p>
251+
</div>
252+
{% endfor %}
253+
```
224254

225255
in the `blog/templates/blog/post_list.html` with this:
226256

227-
<div class="content container">
228-
<div class="row">
229-
<div class="col-md-8">
230-
{% for post in posts %}
231-
<div class="post">
232-
<div class="date">
233-
{{ post.published_date }}
234-
</div>
235-
<h1><a href="">{{ post.title }}</a></h1>
236-
<p>{{ post.text|linebreaks }}</p>
257+
```html
258+
<div class="content container">
259+
<div class="row">
260+
<div class="col-md-8">
261+
{% for post in posts %}
262+
<div class="post">
263+
<div class="date">
264+
{{ post.published_date }}
237265
</div>
238-
{% endfor %}
239-
</div>
266+
<h1><a href="">{{ post.title }}</a></h1>
267+
<p>{{ post.text|linebreaks }}</p>
268+
</div>
269+
{% endfor %}
240270
</div>
241271
</div>
272+
</div>
273+
```
242274

243275
Save those files and refresh your website.
244276

en/django_admin/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ To add, edit and delete posts we've just modeled, we will use Django admin.
44

55
Let's open the `blog/admin.py` file and replace its content with this:
66

7-
from django.contrib import admin
8-
from .models import Post
7+
```python
8+
from django.contrib import admin
9+
from .models import Post
910

10-
admin.site.register(Post)
11+
admin.site.register(Post)
12+
```
1113

1214
As you can see, we import (include) the Post model defined in the previous chapter. To make our model visible on the admin page, we need to register the model with `admin.site.register(Post)`.
1315

0 commit comments

Comments
 (0)