Skip to content

Commit 9ec9c75

Browse files
committed
Filenames over code blocks in the whole tutorial πŸŽ‰ ✨
1 parent 6360be7 commit 9ec9c75

File tree

10 files changed

+75
-67
lines changed

10 files changed

+75
-67
lines changed

β€Žbook.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
1111
12-
"codeblock-filename@0.0.7"
12+
"gitbook-plugin-codeblock-filename@git+https://github.com/aniav/gitbook-plugin-codeblock-filename.git"
1313
],
1414
"pluginsConfig": {
1515
"ga": {

β€Žen/css/README.mdβ€Ž

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ As you saw when we ran `collectstatic` on the server, Django already knows where
4242

4343
We do that by creating a folder called `static` inside the blog app:
4444

45-
```
46-
djangogirls
47-
β”œβ”€β”€ blog
48-
β”‚ β”œβ”€β”€ migrations
49-
β”‚ └── static
50-
└── mysite
45+
```:command-line
46+
djangogirls
47+
β”œβ”€β”€ blog
48+
β”‚ β”œβ”€β”€ migrations
49+
β”‚ └── static
50+
└── mysite
5151
```
5252

5353
Django will automatically find any folders called "static" inside any of your apps' folders, and it will be able to use their contents as static files.
@@ -58,12 +58,12 @@ Django will automatically find any folders called "static" inside any of your ap
5858

5959
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?
6060

61-
```
62-
djangogirls
63-
└─── blog
64-
└─── static
65-
└─── css
66-
└─── blog.css
61+
```:command-line
62+
djangogirls
63+
└─── blog
64+
└─── static
65+
└─── css
66+
└─── blog.css
6767
```
6868

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

β€Žen/django_admin/README.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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-
```python:admin.py
7+
```python:blog/admin.py
88
from django.contrib import admin
99
from .models import Post
1010

β€Žen/django_forms/README.mdβ€Ž

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ Like every important part of Django, forms have their own file: `forms.py`.
1010

1111
We need to create a file with this name in the `blog` directory.
1212

13-
blog
14-
└── forms.py
13+
```:command-line
14+
blog
15+
└── forms.py
16+
```
1517

1618
Ok, let's open it and type the following code:
1719

18-
```python
20+
```python:blog/forms.py
1921
from django import forms
2022

2123
from .models import Post
@@ -43,15 +45,15 @@ So once again we will create: a link to the page, a URL, a view and a template.
4345

4446
It's time to open `blog/templates/blog/base.html`. We will add a link in `div` named `page-header`:
4547

46-
```html
48+
```html:blog/templates/blog/base.html
4749
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
4850
```
4951

5052
Note that we want to call our new view `post_new`.
5153

5254
After adding the line, your html file should now look like this:
5355

54-
```html
56+
```html:blog/templates/blog/base.html
5557
{% load staticfiles %}
5658
<html>
5759
<head>
@@ -84,13 +86,13 @@ After saving and refreshing the page http://127.0.0.1:8000 you will obviously se
8486

8587
We open `blog/urls.py` and add a line:
8688

87-
```python
88-
url(r'^post/new/$', views.post_new, name='post_new'),
89+
```python:blog/urls.py
90+
url(r'^post/new/$', views.post_new, name='post_new'),
8991
```
9092

9193
And the final code will look like this:
9294

93-
```python
95+
```python:blog/urls.py
9496
from django.conf.urls import include, url
9597
from . import views
9698

@@ -107,13 +109,13 @@ After refreshing the site, we see an `AttributeError`, since we don't have `post
107109

108110
Time to open the `blog/views.py` file and add the following lines with the rest of the `from` rows:
109111

110-
```python
112+
```python:blog/views.py
111113
from .forms import PostForm
112114
```
113115

114116
and our *view*:
115117

116-
```python
118+
```python:blog/views.py
117119
def post_new(request):
118120
form = PostForm()
119121
return render(request, 'blog/post_edit.html', {'form': form})
@@ -134,7 +136,7 @@ We need to create a file `post_edit.html` in the `blog/templates/blog` directory
134136

135137
Ok, so let's see how the HTML in `post_edit.html` should look:
136138

137-
```html
139+
```html:blog/templates/blog/post_edit.html
138140
{% extends 'blog/base.html' %}
139141
140142
{% block content %}
@@ -160,7 +162,7 @@ The answer is: nothing. We need to do a little bit more work in our *view*.
160162

161163
Open `blog/views.py` once again. Currently all we have in the `post_new` view is:
162164

163-
```python
165+
```python:blog/views.py
164166
def post_new(request):
165167
form = PostForm()
166168
return render(request, 'blog/post_edit.html', {'form': form})
@@ -170,7 +172,7 @@ When we submit the form, we are brought back to the same view, but this time we
170172

171173
So in our *view* we have two separate situations to handle. First: when we access the page for the first time and we want a blank form. Second: when we go back to the *view* with all form's data we just typed. So we need to add a condition (we will use `if` for that).
172174

173-
```python
175+
```python:blog/views.py
174176
if request.method == "POST":
175177
[...]
176178
else:
@@ -179,15 +181,15 @@ else:
179181

180182
It's time to fill in the dots `[...]`. If `method` is `POST` then we want to construct the `PostForm` with data from the form, right? We will do that with:
181183

182-
```python
184+
```python:blog/views.py
183185
form = PostForm(request.POST)
184186
```
185187

186188
Easy! Next thing is to check if the form is correct (all required fields are set and no incorrect values will be saved). We do that with `form.is_valid()`.
187189

188190
We check if the form is valid and if so, we can save it!
189191

190-
```python
192+
```python:blog/views.py
191193
if form.is_valid():
192194
post = form.save(commit=False)
193195
post.author = request.user
@@ -200,21 +202,21 @@ Basically, we have two things here: we save the form with `form.save` and we add
200202

201203
Finally, it would be awesome if we can immediatelly go to `post_detail` page for newly created blog post, right? To do that we need one more import:
202204

203-
```python
205+
```python:blog/views.py
204206
from django.shortcuts import redirect
205207
```
206208

207209
Add it at the very beginning of your file. And now we can say: go to `post_detail` page for a newly created post.
208210

209-
```python
211+
```python:blog/views.py
210212
return redirect('blog.views.post_detail', pk=post.pk)
211213
```
212214

213215
`blog.views.post_detail` is the name of the view we want to go to. Remember that this *view* requires a `pk` variable? To pass it to the views we use `pk=post.pk`, where `post` is the newly created blog post!
214216

215217
Ok, we talked a lot, but we probably want to see what the whole *view* looks like now, right?
216218

217-
```python
219+
```python:blog/views.py
218220
def post_new(request):
219221
if request.method == "POST":
220222
form = PostForm(request.POST)
@@ -257,13 +259,13 @@ Now we know how to add a new form. But what if we want to edit an existing one?
257259

258260
Open `blog/templates/blog/post_detail.html` and add this line:
259261

260-
```python
262+
```html:blog/templates/blog/post_detail.html
261263
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
262264
```
263265

264266
so that the template will look like:
265267

266-
```html
268+
```html:blog/templates/blog/post_detail.html
267269
{% extends 'blog/base.html' %}
268270
269271
{% block content %}
@@ -282,15 +284,15 @@ so that the template will look like:
282284

283285
In `blog/urls.py` we add this line:
284286

285-
```python
287+
```python:blog/urls.py
286288
url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
287289
```
288290

289291
We will reuse the template `blog/templates/blog/post_edit.html`, so the last missing thing is a *view*.
290292

291293
Let's open a `blog/views.py` and add at the very end of the file:
292294

293-
```python
295+
```python:blog/views.py
294296
def post_edit(request, pk):
295297
post = get_object_or_404(Post, pk=pk)
296298
if request.method == "POST":
@@ -308,13 +310,13 @@ def post_edit(request, pk):
308310

309311
This looks almost exactly the same as our `post_new` view, right? But not entirely. First thing: we pass an extra `pk` parameter from urls. Next: we get the `Post` model we want to edit with `get_object_or_404(Post, pk=pk)` and then, when we create a form we pass this post as an `instance` both when we save the form:
310312

311-
```python
313+
```python:blog/views.py
312314
form = PostForm(request.POST, instance=post)
313315
```
314316

315317
and when we just opened a form with this post to edit:
316318

317-
```python
319+
```python:blog/views.py
318320
form = PostForm(instance=post)
319321
```
320322

@@ -360,7 +362,7 @@ Let's see if all this works on PythonAnywhere. Time for another deploy!
360362

361363
* First, commit your new code, and push it up to Github
362364

363-
```
365+
```:command-line
364366
$ git status
365367
$ git add -A .
366368
$ git status
@@ -370,7 +372,7 @@ $ git push
370372

371373
* Then, in a [PythonAnywhere Bash console](https://www.pythonanywhere.com/consoles/):
372374

373-
```
375+
```:command-line
374376
$ cd my-first-blog
375377
$ source myvenv/bin/activate
376378
(myvenv)$ git pull

β€Žen/django_models/README.mdβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ djangogirls
9292

9393
After creating an application we also need to tell Django that it should use it. We do that in the file `mysite/settings.py`. We need to find `INSTALLED_APPS` and add a line containing `'blog',` just above `)`. So the final product should look like this:
9494

95-
```python:settings.py
95+
```python:mysite/settings.py
9696
INSTALLED_APPS = (
9797
'django.contrib.admin',
9898
'django.contrib.auth',
@@ -110,7 +110,7 @@ In the `blog/models.py` file we define all objects called `Models` - this is a p
110110

111111
Let's open `blog/models.py`, remove everything from it and write code like this:
112112

113-
```python:models.py
113+
```python:blog/models.py
114114
from django.db import models
115115
from django.utils import timezone
116116

β€Žen/django_start_project/README.mdβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ It would be nice to have the correct time on our website. Go to the [wikipedia t
6464

6565
In settings.py, find the line that contains `TIME_ZONE` and modify it to choose your own timezone:
6666

67-
```python:settings.py
67+
```python:mysite/settings.py
6868
TIME_ZONE = 'Europe/Berlin'
6969
```
7070

@@ -73,7 +73,7 @@ Modifying "Europe/Berlin" as appropriate
7373

7474
We'll also need to add a path for static files (we'll find out all about static files and CSS later in the tutorial). Go down to the *end* of the file, and just underneath the `STATIC_URL` entry, add a new one called `STATIC_ROOT`:
7575

76-
```python:settings.py
76+
```python:mysite/settings.py
7777
STATIC_URL = '/static/'
7878
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
7979
```
@@ -85,7 +85,7 @@ There's a lot of different database software that can store data for your site.
8585

8686
This is already set up in this part of your `mysite/settings.py` file:
8787

88-
```python:settings.py
88+
```python:mysite/settings.py
8989
DATABASES = {
9090
'default': {
9191
'ENGINE': 'django.db.backends.sqlite3',

β€Žen/django_urls/README.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Lines that start with `#` are comments - it means that those lines won't be run
3434
The admin URL, which you visited in previous chapter is already here:
3535

3636
```python:mysite/urls.py
37-
url(r'^admin/', include(admin.site.urls)),
37+
url(r'^admin/', include(admin.site.urls)),
3838
```
3939

4040
It means that for every URL that starts with `admin/` Django will find a corresponding *view*. In this case we're including a lot of admin URLs so it isn't all packed into this small file -- it's more readable and cleaner.

0 commit comments

Comments
Β (0)