You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/css/README.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,12 +42,12 @@ As you saw when we ran `collectstatic` on the server, Django already knows where
42
42
43
43
We do that by creating a folder called `static` inside the blog app:
44
44
45
-
```
46
-
djangogirls
47
-
βββ blog
48
-
β βββ migrations
49
-
β βββ static
50
-
βββ mysite
45
+
```:command-line
46
+
djangogirls
47
+
βββ blog
48
+
β βββ migrations
49
+
β βββ static
50
+
βββ mysite
51
51
```
52
52
53
53
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
58
58
59
59
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?
60
60
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
67
67
```
68
68
69
69
Time to write some CSS! Open up the `blog/static/css/blog.css` file in your code editor.
@@ -170,7 +172,7 @@ When we submit the form, we are brought back to the same view, but this time we
170
172
171
173
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).
172
174
173
-
```python
175
+
```python:blog/views.py
174
176
if request.method =="POST":
175
177
[...]
176
178
else:
@@ -179,15 +181,15 @@ else:
179
181
180
182
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:
181
183
182
-
```python
184
+
```python:blog/views.py
183
185
form = PostForm(request.POST)
184
186
```
185
187
186
188
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()`.
187
189
188
190
We check if the form is valid and if so, we can save it!
189
191
190
-
```python
192
+
```python:blog/views.py
191
193
if form.is_valid():
192
194
post = form.save(commit=False)
193
195
post.author = request.user
@@ -200,21 +202,21 @@ Basically, we have two things here: we save the form with `form.save` and we add
200
202
201
203
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:
202
204
203
-
```python
205
+
```python:blog/views.py
204
206
from django.shortcuts import redirect
205
207
```
206
208
207
209
Add it at the very beginning of your file. And now we can say: go to `post_detail` page for a newly created post.
`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!
214
216
215
217
Ok, we talked a lot, but we probably want to see what the whole *view* looks like now, right?
216
218
217
-
```python
219
+
```python:blog/views.py
218
220
defpost_new(request):
219
221
if request.method =="POST":
220
222
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?
257
259
258
260
Open `blog/templates/blog/post_detail.html` and add this line:
We will reuse the template `blog/templates/blog/post_edit.html`, so the last missing thing is a *view*.
290
292
291
293
Let's open a `blog/views.py` and add at the very end of the file:
292
294
293
-
```python
295
+
```python:blog/views.py
294
296
defpost_edit(request, pk):
295
297
post = get_object_or_404(Post, pk=pk)
296
298
if request.method =="POST":
@@ -308,13 +310,13 @@ def post_edit(request, pk):
308
310
309
311
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:
310
312
311
-
```python
313
+
```python:blog/views.py
312
314
form = PostForm(request.POST, instance=post)
313
315
```
314
316
315
317
and when we just opened a form with this post to edit:
316
318
317
-
```python
319
+
```python:blog/views.py
318
320
form = PostForm(instance=post)
319
321
```
320
322
@@ -360,7 +362,7 @@ Let's see if all this works on PythonAnywhere. Time for another deploy!
360
362
361
363
* First, commit your new code, and push it up to Github
362
364
363
-
```
365
+
```:command-line
364
366
$ git status
365
367
$ git add -A .
366
368
$ git status
@@ -370,7 +372,7 @@ $ git push
370
372
371
373
* Then, in a [PythonAnywhere Bash console](https://www.pythonanywhere.com/consoles/):
Copy file name to clipboardExpand all lines: en/django_models/README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,7 +92,7 @@ djangogirls
92
92
93
93
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:
94
94
95
-
```python:settings.py
95
+
```python:mysite/settings.py
96
96
INSTALLED_APPS= (
97
97
'django.contrib.admin',
98
98
'django.contrib.auth',
@@ -110,7 +110,7 @@ In the `blog/models.py` file we define all objects called `Models` - this is a p
110
110
111
111
Let's open `blog/models.py`, remove everything from it and write code like this:
Copy file name to clipboardExpand all lines: en/django_start_project/README.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ It would be nice to have the correct time on our website. Go to the [wikipedia t
64
64
65
65
In settings.py, find the line that contains `TIME_ZONE` and modify it to choose your own timezone:
66
66
67
-
```python:settings.py
67
+
```python:mysite/settings.py
68
68
TIME_ZONE='Europe/Berlin'
69
69
```
70
70
@@ -73,7 +73,7 @@ Modifying "Europe/Berlin" as appropriate
73
73
74
74
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`:
75
75
76
-
```python:settings.py
76
+
```python:mysite/settings.py
77
77
STATIC_URL='/static/'
78
78
STATIC_ROOT= os.path.join(BASE_DIR, 'static')
79
79
```
@@ -85,7 +85,7 @@ There's a lot of different database software that can store data for your site.
85
85
86
86
This is already set up in this part of your `mysite/settings.py` file:
Copy file name to clipboardExpand all lines: en/django_urls/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ Lines that start with `#` are comments - it means that those lines won't be run
34
34
The admin URL, which you visited in previous chapter is already here:
35
35
36
36
```python:mysite/urls.py
37
-
url(r'^admin/', include(admin.site.urls)),
37
+
url(r'^admin/', include(admin.site.urls)),
38
38
```
39
39
40
40
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