Skip to content

Commit d37e8f3

Browse files
committed
Add a bit of security to the "add new post" link
The link is awesome but, if followers stop at the end of the official tutorial, they have a large hole in their site that allows third-parties to post to their app for them. This is a minimal step to make it more secure for those that don't get to the end of the second tutorial extension.
1 parent 8a1f948 commit d37e8f3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

en/django_forms/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@ Congratulations! Your application is getting more and more complete!
332332

333333
If you need more information about Django forms you should read the documentation: https://docs.djangoproject.com/en/1.8/topics/forms/
334334

335+
## Security
336+
337+
Being able to create new posts just by clicking a link is awesome! But, right now, anyone that visits your site will be able to post 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 far anyone else.
338+
339+
In `blog/templates/blog/base.html`, find our `page-header` `div` and the anchor tag you put in there earlier. It should look like this:
340+
341+
```html
342+
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
343+
```
344+
345+
We're going to add another `{% if %}` tag to this which will make the link only show up for users that are logged into the admin. Right now, that's just you! Change the `<a>` tag to look like this:
346+
347+
```html
348+
{% if user.is_authenticated %}
349+
<a href="{% url 'post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>
350+
{% endif %}
351+
```
352+
353+
This `{% if %}` will cause the link to only be sent to the browser if the user requesting the page is logged in. This doesn't protect the creation of new posts completely, but it's a good first step. We'll cover more security in the extension lessons.
354+
355+
Since you're likely logged in, if you refresh the page, you won't see anything different. Load the page in a new browser or an incognito window, though, and you'll see that the link doesn't show up!
356+
335357
## One more thing: deploy time!
336358

337359
Let's see if all this works on PythonAnywhere. Time for another deploy!

0 commit comments

Comments
 (0)