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
You might have noticed that you didn't have to use your password apart from back when we used the admin-interface. And you might notice that this means that everyone can add or edit posts in your blog. I don't know about you, but I don't want just anyone to post on my blog. So lets do something about it.
4
+
5
+
## Authorizing add/edit of posts
6
+
7
+
First lets make things secure. We will protect our `post_new`, `post_edit` and `post_publish` views so that only logged-in users can access them. Django ships with some nice helpers for that using a kind-of advanced topic of _decorators_. Don't worry about the technicalities now, you can read up on these later. The decorator to use is shipped in Django in a module `django.contrib.auth.decorators` and is named `login_required`.
8
+
9
+
So edit your `mysite/views.py` and add these lines at the top where all the imports are:
10
+
11
+
```
12
+
from django.contrib.auth.decorators import login_required
13
+
```
14
+
15
+
Then add a line before each of the `post_new`, `post_edit` and `post_publish` like the following:
16
+
17
+
```
18
+
@login_required
19
+
def post_new(request):
20
+
[...]
21
+
```
22
+
23
+
Thats it! Now try again to access `http://localhost:8000/post/new/`, notice the difference?
24
+
25
+
> If you just got the empty form, you are probably still logged in from the chapter on the admin-interface. Go to `http://localhost:8000/admin/logout/` to log out, then goto `http://localhost:8000/post/new` again.
26
+
27
+
You should get one of the beloved errors again. This one is quite interesting actually: The decorator we added before will redirect you to the login page. But that isn't available yet, so it raises a "Page not found (404)".
28
+
29
+
Don't forget to add the decorator from above to `post_edit` and `post_publish` too.
30
+
31
+
Horray, we reached part of the goal! Other people can't just create posts on our blog anymore. Unfortunately we can't create posts anymore too. So lets fix that next.
32
+
33
+
## Login users
34
+
35
+
Now we could try to do lots of magic stuff to implement users and passwords and authentication but doing this kind of stuff right is rather complicated. And as Django is "batteries included", we will make further use of the authentication stuff provided.
36
+
37
+
In your `mysite/urls.py` add an url `url(r'^accounts/login/$', 'django.contrib.auth.views.login')`. So the file should now look similar to this:
38
+
39
+
```
40
+
from django.conf.urls import patterns, include, url
<input type="hidden" name="next" value="{{ next }}" />
78
+
</form>
79
+
80
+
{% endblock %}
81
+
```
82
+
83
+
You will see that this also makes use of our base-template for the overall look and feel of your blog.
84
+
85
+
The nice thing here is that this _just works[TM]_. We don't have to deal with handling of the forms submission nor with passwords and securing them. Only one thing is left here, we should add a setting to `mysite/settings.py`:
86
+
87
+
```
88
+
LOGIN_REDIRECT_URL = '/'
89
+
```
90
+
91
+
Now when the login is accessed directly, it will redirect successful login to the top level index.
92
+
93
+
## Improving the layout
94
+
95
+
So now we made sure that only authorized users (ie. us) can add, edit or publish posts. But still everyone gets to view the buttons to add or edit posts, lets hide these for users that aren't logged in. For this we need to edit the templates, so lets start with the base template from `mysite/templates/mysite/base.html`:
You might recognize the pattern here. There is an if-condition inside the template that checks for authenticated users to show the edit-buttons. Otherwise it shows a login button.
120
+
121
+
*Homework*: Edit the template `blog/templates/blog/post_detail.html` to only show the edit-buttons for authenticated users.
122
+
123
+
## More on authenticated users
124
+
125
+
Lets add some nice sugar to our templates while we are at it. First we will add some stuff to show that we are logged in. Edit `mysite/templates/mysite/base.html` like this:
This adds a nice "Hello <username>" to remind us who we are and that we are authenticated. Also this adds link to log out of the blog. But as you might notice this isn't working yet. Oh nooz, we broke the internez! Lets fix it!
141
+
142
+
We decided to rely on django to handle log-in, lets see if Django can also handle log-out for us. Check https://docs.djangoproject.com/en/1.6/topics/auth/default/ and see if you find something.
143
+
144
+
Done reading? You should by now think about adding a url (in `mysite/urls.py`) pointing to the `django.contrib.auth.views.logout` view. Like that:
145
+
146
+
```
147
+
from django.conf.urls import patterns, include, url
Copy file name to clipboardExpand all lines: css/README.md
+23-17Lines changed: 23 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Our blog still looks pretty ugly, right? Time to make it nice! We will use CSS f
6
6
7
7
Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a website written in markup language (like HTML). Treat it as a make-up for our webpage ;).
8
8
9
-
But we don't want to start from scratch again, right? We will, again, use something that has already been done by programmers and released in the Internet for free. You know, inventing a wheel once again is no fun.
9
+
But we don't want to start from scratch again, right? We will, again, use something that has already been done by programmers and released in the Internet for free. You know, reinventing the wheel is no fun.
10
10
11
11
## Let's use Bootstrap!
12
12
@@ -16,13 +16,14 @@ It was written by programmers who worked for Twitter and is now developed by vol
16
16
17
17
## Install Boostrap
18
18
19
-
To install Bootstrap, you need to add this to your `<head>` in you`.html` file (`blog/templates/blog/post_list.html`):
19
+
To install Bootstrap, you need to add this to your `<head>` in your`.html` file (`blog/templates/blog/post_list.html`):
Then just go ahead, open your website and refresh page. Here it is!
25
+
This doesn't add any files to your project. It just points to files that exist on the internet.
26
+
Just go ahead, open your website and refresh the page. Here it is!
26
27
27
28

28
29
@@ -41,19 +42,17 @@ First, we need to create a folder to store our static files in. Go ahead and cre
41
42
static
42
43
manage.py
43
44
44
-
Now we need to tell Django how it can find it. Open up the `mysite/settings.py` file, scroll to the bottom of it and add the following lines:
45
+
Now we need to tell Django where it can find it. Open up the `mysite/settings.py` file, scroll to the bottom of it and add the following lines:
45
46
46
47
STATICFILES_DIRS = (
47
48
os.path.join(BASE_DIR, "static"),
48
49
)
49
50
50
51
This way Django will know where your static files are.
51
52
52
-
That's it! Time to see if it works :)
53
-
54
53
## Your first CSS file!
55
54
56
-
First things first: let's create a CSS file now. Create a new folder called `css` inside your `static` folder. Then create a new file called `blog.css` inside this `css` directory. Ready?
55
+
Let's create a CSS file now, to add your own style to your web-page. Create a new folder called `css` inside your `static` folder. Then create a new file called `blog.css` inside this `css` directory. Ready?
57
56
58
57
static
59
58
└───css
@@ -63,21 +62,27 @@ Time to write some CSS! Open up the `static/css/blog.css` file in your code edit
63
62
64
63
We won't be going too deep into customizing and learning about CSS here, because it's pretty easy and you can learn it on your own after this workshop. We really recommend doing this [Codeacademy HTML & CSS course](http://www.codecademy.com/tracks/web) to learn everything you need to know about making your websites more pretty with CSS.
65
64
66
-
But let's do at least a little. Maybe we could change the color of our header? To understand colors, computer use special codes. They start with `#` and are followed by 6 letters (A-F) and numbers (0-9). You can find color codes for example here: http://www.colorpicker.com/
65
+
But let's do at least a little. Maybe we could change the color of our header? To understand colors, computers use special codes. They start with `#` and are followed by 6 letters (A-F) and numbers (0-9). You can find color codes for example here: http://www.colorpicker.com/. You may also use [predefined colors](http://www.w3schools.com/cssref/css_colornames.asp), such as `red` and `green`.
67
66
68
67
In your `static/css/blog.css` file you should add following code:
69
68
70
69
h1 a {
71
70
color: #FCA205;
72
71
}
73
72
74
-
`a` inside of `h1` (i.e. when we have in code something like: `<h1><a href="">link</a></h1>`) is the tag we're applying our styles to, and we're telling it to change color to `#FCA205`. Of course, you can put your own color here!
73
+
`h1 a` is a CSS Selector. `a` element inside of `h1` element (i.e. when we have in code something like: `<h1><a href="">link</a></h1>`) is the tag we're applying our styles to, and we're telling it to change color to `#FCA205`, which is orange. Of course, you can put your own color here!
74
+
75
+
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 element class or the element id. Class and id are names you give the element by yourself. Classes define groups of elements, and ids point out 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`:
Read about [CSS Selectors in w3schools](http://www.w3schools.com/cssref/css_selectors.asp).
75
80
76
81
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:
77
82
78
83
{% load staticfiles %}
79
84
80
-
We're just loading static files here :). Then, between the `<head>` and `</head>` add this line:
85
+
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:
@@ -127,7 +132,7 @@ Maybe we can customize the font in our header? Paste this into your `<head>` in
127
132
128
133
This line will import a font called *Lobster* from Google Fonts (https://www.google.com/fonts).
129
134
130
-
Now add this line in `static/css/blog.css` and refresh the page:
135
+
Now add the line `font-family: 'Lobster';`in the CSS file `mysite/static/css/blog.css` inside the `h1 a` declaration block (the code between the braces `{` and `}`) and refresh the page:
131
136
132
137
h1 a {
133
138
color: #FCA205;
@@ -138,7 +143,8 @@ Now add this line in `static/css/blog.css` and refresh the page:
138
143
139
144
Great!
140
145
141
-
CSS has a concept of classes, which basically allows you to name a part of our HTML code and apply styles only to this part, not affecting others. It's super helpful if you have two divs, but they're doing something very different (like your header and your post), so you don't want them to look the same.
146
+
147
+
As mentioned above, CSS has a concept of classes, which basically allows you to name a part of the HTML code and apply styles only to this part, not affecting others. It's super helpful if you have two divs, but they're doing something very different (like your header and your post), so you don't want them to look the same.
142
148
143
149
Go ahead and name some parts of the HTML code. Add a class called `page-header` to your `div` that contains header, like this:
144
150
@@ -154,7 +160,7 @@ And now add a class `post` to your `div` containing blogposts.
154
160
<p>{{ post.text }}</p>
155
161
</div>
156
162
157
-
All right. We have only one day, so we need to speed things up a little! We can't explain you every little detail about CSS. For now just copy and paste following code into your `static/css/blog.css` file:
163
+
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 `mysite/static/css/blog.css` file:
158
164
159
165
.page-header {
160
166
background-color: #ff9400;
@@ -204,7 +210,7 @@ All right. We have only one day, so we need to speed things up a little! We can'
204
210
color: #000000;
205
211
}
206
212
207
-
Then also replace this:
213
+
Then surround the HTML code which desplayes the posts with declarations of classes. Replace this:
208
214
209
215
{% for post in posts %}
210
216
<div class="post">
@@ -238,7 +244,7 @@ Wohoo! Looks awesome, right? The code we just pasted is not really so hard to un
238
244
239
245
Don't be afraid to tinker with this CSS a little bit and try to change some things. If you break something, don't worry, you can always undo this!
240
246
241
-
Anyway, we really recommend taking this free online [Codeacademy HTML & CSS course](http://www.codecademy.com/tracks/web) as a post-workshop homework to learn everything you need to know about making your websites more pretty with CSS.
247
+
Anyway, we really recommend taking this free online [Codeacademy HTML & CSS course](http://www.codecademy.com/tracks/web) as a post-workshop homework to learn everything you need to know about making your websites prettier with CSS.
After the installation is finished, run this command:
17
+
After the installation is finished, go to the `mysite` directory and run this command:
18
18
19
19
(myvenv) $ pip freeze > requirements.txt
20
20
21
21
This will create a file called `requirements.txt` with a list of your installed packages (i.e. Python libraries that you are using, for example Django :)).
22
22
23
23
Open this file and add the following line at the bottom:
24
-
24
+
25
25
pyscopg2==2.5.3
26
26
27
27
This line is needed for your application to work on Heroku.
@@ -73,7 +73,7 @@ Another thing we need to do is modify our website's `settings.py` file. Open `my
73
73
ALLOWED_HOSTS = ['*']
74
74
75
75
STATIC_ROOT = 'staticfiles'
76
-
76
+
77
77
DEBUG = False
78
78
79
79
At the end of the `mysite/settings.py`, copy and paste this:
@@ -90,7 +90,7 @@ Then save the file.
90
90
## mysite/wsgi.py
91
91
92
92
Open the `mysite/wsgi.py` file and add these lines at the end:
The command above will create a folder called `myvenv` that contains our virtual environment (basically bunch of folders and files). All we want to do now is starting it by running:
@@ -50,6 +61,12 @@ on Windows, or:
50
61
51
62
on OS X and Linux.
52
63
64
+
> __NOTE:__ sometimes `source` might not be available. In those cases try doing this instead:
65
+
66
+
> ~/djangogirls$ . myvenv/bin/activate
67
+
68
+
69
+
53
70
You will know that you have `virtualenv` started when you see that the prompt in your console looks like:
0 commit comments