Skip to content

Commit 937d95f

Browse files
authored
Merge pull request #1681 from nikhiljohn10/migration-html5-bootstrap4
Migration to html5 and bootstrap4
2 parents 078707c + a9dbbf5 commit 937d95f

File tree

7 files changed

+196
-126
lines changed

7 files changed

+196
-126
lines changed

en/css/README.md

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ To install Bootstrap, open up your `.html` file in the code editor and add this
2020

2121
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
2222
```html
23-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
24-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
23+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
2524
```
2625

2726
This doesn't add any files to your project. It just points to files that exist on the Internet. So go ahead, open your website and refresh the page. Here it is!
@@ -68,7 +67,7 @@ djangogirls
6867

6968
Time to write some CSS! Open up the `blog/static/css/blog.css` file in your code editor.
7069

71-
We won't be going too deep into customizing and learning about CSS here. There is a recommendation for a free CSS course at the end of this page if you would like to learn more.
70+
We won't be going too deep into customizing and learning about CSS here. There is a recommendation for a free CSS course at the end of this page if you would like to learn more.
7271

7372
But let's do at least a little. Maybe we could change the color of our headers?
7473
To understand colors, computers use special codes. These codes start with `#` followed by 6 letters (A–F) and numbers (0–9). For example, the code for blue is `#0000FF`. You can find the color codes for many colors here: http://www.colorpicker.com/. You may also use [predefined colors](http://www.w3schools.com/colors/colors_names.asp), such as `red` and `green`.
@@ -78,7 +77,7 @@ In your `blog/static/css/blog.css` file you should add the following code:
7877
{% filename %}blog/static/css/blog.css{% endfilename %}
7978
```css
8079
h1 a, h2 a {
81-
color: #FF8833;
80+
color: #C25100;
8281
}
8382

8483
```
@@ -116,24 +115,24 @@ Your file should now look like this:
116115
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
117116
```html
118117
{% load static %}
118+
<!DOCTYPE html>
119119
<html>
120120
<head>
121121
<title>Django Girls blog</title>
122-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
123-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
122+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
124123
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
125124
</head>
126125
<body>
127-
<div>
126+
<header>
128127
<h1><a href="/">Django Girls Blog</a></h1>
129-
</div>
128+
</header>
130129

131130
{% for post in posts %}
132-
<div>
133-
<p>published: {{ post.published_date }}</p>
131+
<article>
132+
<time>published: {{ post.published_date }}</time>
134133
<h2><a href="">{{ post.title }}</a></h2>
135134
<p>{{ post.text|linebreaksbr }}</p>
136-
</div>
135+
</article>
137136
{% endfor %}
138137
</body>
139138
</html>
@@ -182,24 +181,26 @@ Great!
182181

183182
As mentioned above, CSS has a concept of classes. These allow you to name a part of the HTML code and apply styles only to this part, without affecting other parts. This can be super helpful! Maybe you have two divs that are doing something different (like your header and your post). A class can help you make them look different.
184183

185-
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:
184+
Go ahead and name some parts of the HTML code. Replace the `header` that contains your header with the following:
186185

187186
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
188187
```html
189-
<div class="page-header">
190-
<h1><a href="/">Django Girls Blog</a></h1>
191-
</div>
188+
<header class="page-header">
189+
<div class="container">
190+
<h1><a href="/">Django Girls Blog</a></h1>
191+
</div>
192+
</header>
192193
```
193194

194-
And now add a class `post` to your `div` containing a blog post.
195+
And now add a class `post` to your `article` containing a blog post.
195196

196197
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
197198
```html
198-
<div class="post">
199-
<p>published: {{ post.published_date }}</p>
199+
<article class="post">
200+
<time>published: {{ post.published_date }}</time>
200201
<h2><a href="">{{ post.title }}</a></h2>
201202
<p>{{ post.text|linebreaksbr }}</p>
202-
</div>
203+
</article>
203204
```
204205

205206
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 that can help you understand the following code. For now, copy and paste it into your `blog/static/css/blog.css` file:
@@ -209,20 +210,23 @@ We will now add declaration blocks to different selectors. Selectors starting wi
209210
.page-header {
210211
background-color: #C25100;
211212
margin-top: 0;
213+
margin-bottom: 40px;
212214
padding: 20px 20px 20px 40px;
213215
}
214216

215-
.page-header h1, .page-header h1 a, .page-header h1 a:visited, .page-header h1 a:active {
217+
.page-header h1,
218+
.page-header h1 a,
219+
.page-header h1 a:visited,
220+
.page-header h1 a:active {
216221
color: #ffffff;
217222
font-size: 36pt;
218223
text-decoration: none;
219224
}
220225

221-
.content {
222-
margin-left: 40px;
223-
}
224-
225-
h1, h2, h3, h4 {
226+
h1,
227+
h2,
228+
h3,
229+
h4 {
226230
font-family: 'Lobster', cursive;
227231
}
228232

@@ -234,11 +238,14 @@ h1, h2, h3, h4 {
234238
float: right;
235239
}
236240

237-
.post-form textarea, .post-form input {
241+
.post-form textarea,
242+
.post-form input {
238243
width: 100%;
239244
}
240245

241-
.top-menu, .top-menu:hover, .top-menu:visited {
246+
.top-menu,
247+
.top-menu:hover,
248+
.top-menu:visited {
242249
color: #ffffff;
243250
float: right;
244251
font-size: 26pt;
@@ -249,43 +256,61 @@ h1, h2, h3, h4 {
249256
margin-bottom: 70px;
250257
}
251258

252-
.post h2 a, .post h2 a:visited {
259+
.post h2 a,
260+
.post h2 a:visited {
253261
color: #000000;
254262
}
263+
264+
.post > .date,
265+
.post > .actions {
266+
float: right;
267+
}
268+
269+
.btn-default,
270+
.btn-default:visited {
271+
color: #C25100;
272+
background: none;
273+
border-color: #C25100;
274+
}
275+
276+
.btn-default:hover {
277+
color: #FFFFFF;
278+
background-color: #C25100;
279+
}
255280
```
256281

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

259284
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
260285
```html
261286
{% for post in posts %}
262-
<div class="post">
263-
<p>published: {{ post.published_date }}</p>
287+
<article class="post">
288+
<time>published: {{ post.published_date }}</time>
264289
<h2><a href="">{{ post.title }}</a></h2>
265290
<p>{{ post.text|linebreaksbr }}</p>
266-
</div>
291+
</article>
267292
{% endfor %}
268293
```
269294

270295
in the `blog/templates/blog/post_list.html` with this:
271296

272297
{% filename %}blog/templates/blog/post_list.html{% endfilename %}
273298
```html
274-
<div class="content container">
299+
<main class="container">
275300
<div class="row">
276-
<div class="col-md-8">
301+
<div class="col">
277302
{% for post in posts %}
278-
<div class="post">
279-
<div class="date">
280-
<p>published: {{ post.published_date }}</p>
281-
</div>
303+
<article class="post">
304+
<time class="date">
305+
{{ post.published_date }}
306+
</time>
282307
<h2><a href="">{{ post.title }}</a></h2>
283308
<p>{{ post.text|linebreaksbr }}</p>
284-
</div>
309+
</article>
285310
{% endfor %}
286311
</div>
287312
</div>
288-
</div>
313+
</main>
289314
```
290315

291316
Save those files and refresh your website.
@@ -300,4 +325,3 @@ Don't be afraid to tinker with this CSS a little bit and try to change some thin
300325
We really recommend taking the free online courses "Basic HTML & HTML5" and "Basic CSS" on [freeCodeCamp](https://learn.freecodecamp.org/). They can help you learn all about making your websites prettier with HTML and CSS.
301326

302327
Ready for the next chapter?! :)
303-

en/django_forms/README.md

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,53 @@ So once again we will create a link to the page, a URL, a view and a template.
4444

4545
## Link to a page with the form
4646

47-
It's time to open `blog/templates/blog/base.html` in the code editor. In the `div` named `page-header`, we will add a link:
47+
Before we add the link, we need some icons to use as buttons for the link. For this tutorial, download [file-earmark-plus.svg](https://raw.githubusercontent.com/twbs/icons/main/icons/file-earmark-plus.svg) and save it in the folder `blog/templates/blog/icons/`
48+
49+
> Note: To download the SVG image, open the context menu on the link (usually by right-clicking on it) and select "Save link as". In the dialog asking you where to save the file, navigate to the `djangogirls` directory of your Django project, and within that to subdirectory `blog/templates/blog/icons/`, and save the file there.
50+
51+
It's time to open `blog/templates/blog/base.html` in the code editor. Now we can use this icon file inside the base template as follow. In the `div` tag inside `header` section, we will add a link before `h1` tag:
4852

4953
{% filename %}blog/templates/blog/base.html{% endfilename %}
5054
```html
51-
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
55+
<a href="{% url 'post_new' %}" class="top-menu">
56+
{% include './icons/file-earmark-plus.svg' %}
57+
</a>
5258
```
5359

54-
Note that we want to call our new view `post_new`. The class `"glyphicon glyphicon-plus"` is provided by the bootstrap theme we are using, and will display a plus sign for us.
60+
Note that we want to call our new view `post_new`. The [SVG icon](https://icons.getbootstrap.com/icons/file-earmark-plus/) is provided by the [Bootstrap Icons](https://icons.getbootstrap.com/) and it will display a page icon with plus sign. We use a Django template directive called `include`. This will inject the file's content into the Django template. The web browser knows how to handle this type of content without any further processing.
61+
62+
> You can download all the Bootstrap icons [here](https://github.com/twbs/icons/releases/download/v1.1.0/bootstrap-icons-1.1.0.zip). Unzip the file and copy all the SVG image files into a new folder inside `blog/templates/blog/` called `icons`. That way you can access an icon like `pencil-fill.svg` using the file path `blog/templates/blog/icons/pencil-fill.svg`
5563
56-
After adding the line, your HTML file should now look like this:
64+
After editing the line, your HTML file should now look like this:
5765

5866
{% filename %}blog/templates/blog/base.html{% endfilename %}
5967
```html
6068
{% load static %}
69+
<!DOCTYPE html>
6170
<html>
6271
<head>
6372
<title>Django Girls blog</title>
64-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
65-
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
73+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
6674
<link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
6775
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
6876
</head>
6977
<body>
70-
<div class="page-header">
71-
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
72-
<h1><a href="/">Django Girls Blog</a></h1>
73-
</div>
74-
<div class="content container">
78+
<header class="page-header">
79+
<div class="container">
80+
<a href="{% url 'post_new' %}" class="top-menu">
81+
{% include './icons/file-earmark-plus.svg' %}
82+
</a>
83+
<h1><a href="/">Django Girls Blog</a></h1>
84+
</div>
85+
</header>
86+
<main class="content container">
7587
<div class="row">
76-
<div class="col-md-8">
88+
<div class="col">
7789
{% block content %}
7890
{% endblock %}
7991
</div>
8092
</div>
81-
</div>
93+
</main>
8294
</body>
8395
</html>
8496
```
@@ -98,7 +110,7 @@ And the final code will look like this:
98110

99111
{% filename %}blog/urls.py{% endfilename %}
100112
```python
101-
from django.urls import path
113+
from django.urls import path
102114
from . import views
103115

104116
urlpatterns = [
@@ -271,11 +283,17 @@ Django is taking care to validate that all the fields in our form are correct. I
271283

272284
Now we know how to add a new post. But what if we want to edit an existing one? This is very similar to what we just did. Let's create some important things quickly. (If you don't understand something, you should ask your coach or look at the previous chapters, since we covered all these steps already.)
273285

274-
Open `blog/templates/blog/post_detail.html` in the code editor and add the line
286+
First, let's save the icon which represents the edit button. Download [pencil-fill.svg](https://raw.githubusercontent.com/twbs/icons/main/icons/pencil-fill.svg) and save it to the location `blog/templates/blog/icons/`.
287+
288+
Open `blog/templates/blog/post_detail.html` in the code editor and add the following code inside `article` tag:
275289

276290
{% filename %}blog/templates/blog/post_detail.html{% endfilename %}
277291
```html
278-
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
292+
<aside class="actions">
293+
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
294+
{% include './icons/pencil-fill.svg' %}
295+
</a>
296+
</aside>
279297
```
280298

281299
so that the template will look like this:
@@ -285,16 +303,20 @@ so that the template will look like this:
285303
{% extends 'blog/base.html' %}
286304

287305
{% block content %}
288-
<div class="post">
306+
<article class="post">
307+
<aside class="actions">
308+
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
309+
{% include './icons/pencil-fill.svg' %}
310+
</a>
311+
</aside>
289312
{% if post.published_date %}
290-
<div class="date">
313+
<time class="date">
291314
{{ post.published_date }}
292-
</div>
315+
</time>
293316
{% endif %}
294-
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
295317
<h2>{{ post.title }}</h2>
296318
<p>{{ post.text|linebreaksbr }}</p>
297-
</div>
319+
</article>
298320
{% endblock %}
299321
```
300322

@@ -358,19 +380,23 @@ If you need more information about Django forms, you should read the documentati
358380

359381
Being able to create new posts by clicking a link is awesome! But right now, anyone who visits your site will be able to make a new blog post, and that's probably not something you want. Let's make it so the button shows up for you but not for anyone else.
360382

361-
Open `blog/templates/blog/base.html` in the code editor, find our `page-header` `div` and the anchor tag you put in there earlier. It should look like this:
383+
Open `blog/templates/blog/base.html` in the code editor, find our `div` inside `header` and the anchor tag you put in there earlier. It should look like this:
362384

363385
{% filename %}blog/templates/blog/base.html{% endfilename %}
364386
```html
365-
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
387+
<a href="{% url 'post_new' %}" class="top-menu">
388+
{% include './icons/file-earmark-plus.svg' %}
389+
</a>
366390
```
367391

368392
We're going to add another `{% if %}` tag to this, which will make the link show up only for users who are logged into the admin. Right now, that's just you! Change the `<a>` tag to look like this:
369393

370394
{% filename %}blog/templates/blog/base.html{% endfilename %}
371395
```html
372396
{% if user.is_authenticated %}
373-
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
397+
<a href="{% url 'post_new' %}" class="top-menu">
398+
{% include './icons/file-earmark-plus.svg' %}
399+
</a>
374400
{% endif %}
375401
```
376402

@@ -382,15 +408,19 @@ Open `blog/templates/blog/post_detail.html` in the code editor and find this lin
382408

383409
{% filename %}blog/templates/blog/post_detail.html{% endfilename %}
384410
```html
385-
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
411+
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
412+
{% include './icons/pencil-fill.svg' %}
413+
</a>
386414
```
387415

388416
Change it to this:
389417

390418
{% filename %}blog/templates/blog/post_detail.html{% endfilename %}
391419
```html
392420
{% if user.is_authenticated %}
393-
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
421+
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
422+
{% include './icons/pencil-fill.svg' %}
423+
</a>
394424
{% endif %}
395425
```
396426

0 commit comments

Comments
 (0)