Skip to content

Commit 9d9671c

Browse files
committed
fix a couple broken links and add new code examples
1 parent d5b5ace commit 9d9671c

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
lines changed

content/pages/examples/django/django-code-examples.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ following pages:
206206
Additional example code found within gadget-board:
207207

208208
* [django.conf.urls url](/django-conf-urls-url-examples.html)
209+
* [django.contrib admin](/django-contrib-admin-examples.html)
210+
* [django.contrib.auth.hashers make_password](/django-contrib-auth-hashers-make-password-examples.html)
209211

210212

211213
### register
@@ -232,5 +234,5 @@ under the
232234
Example code from wagtail shown on these pages:
233235

234236
* [django.conf.urls url](/django-conf-urls-url-examples.html)
235-
237+
* [django.http Http404](/django-http-http404-examples.html)
236238

content/pages/examples/django/django-contrib-admin.markdown

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,23 @@ from ..models import File
438438

439439
~~FileAdmin.fieldsets = FileAdmin.build_fieldsets()
440440
```
441+
442+
443+
## Example 5 from gadget-board
444+
[gadget-board](https://github.com/mik4el/gadget-board) is a
445+
[Django](/django.html),
446+
[Django REST Framework (DRF)](/django-rest-framework-drf.html) and
447+
[Angular](/angular.html) web application that is open source under the
448+
[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE).
449+
450+
[**gadget-board / web / authentication / admin.py**](https://github.com/mik4el/gadget-board/blob/master/web/authentication/admin.py)
451+
452+
```python
453+
~~from django.contrib import admin
454+
from .models import Account
455+
456+
457+
~~@admin.register(Account)
458+
~~class AccountAdmin(admin.ModelAdmin):
459+
readonly_fields = ('created_at','updated_at',)
460+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
title: django.contrib.auth.hashers make_password Python Code Examples
2+
category: page
3+
slug: django-contrib-auth-hashers-make-password-examples
4+
sortorder: 50035
5+
toc: False
6+
sidebartitle: django.contrib.auth.hashers make_password
7+
meta: Python code examples for the Django function make_password from the django.contrib.auth.hashers module.
8+
9+
10+
[Django](/django.html)'s
11+
[make_password](https://docs.djangoproject.com/en/dev/topics/auth/passwords/#django.contrib.auth.hashers.make_password)
12+
([source code](https://github.com/django/django/blob/master/django/contrib/auth/hashers.py))
13+
function converts a plain-text password into a hash that is appropriate
14+
for storing in a [persistent database](/databases.html).
15+
16+
You definitely do not want to try to roll your own encryption and hashing
17+
functions for storing passwords when this function already exists.
18+
19+
20+
## Example 1 from gadget-board
21+
[gadget-board](https://github.com/mik4el/gadget-board) is a
22+
[Django](/django.html),
23+
[Django REST Framework (DRF)](/django-rest-framework-drf.html) and
24+
[Angular](/angular.html) web application that is open source under the
25+
[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE).
26+
27+
[**gadget-board / web / authentication / views.py**](https://github.com/mik4el/gadget-board/blob/master/web/authentication/views.py)
28+
29+
```python
30+
from rest_framework import permissions, viewsets, status
31+
from rest_framework.response import Response
32+
from rest_framework_jwt.settings import api_settings
33+
~~from django.contrib.auth.hashers import make_password
34+
35+
from .models import Account
36+
from .permissions import IsAccountOwner
37+
from .serializers import AccountSerializer
38+
39+
40+
class AccountViewSet(viewsets.ModelViewSet):
41+
lookup_field = 'username'
42+
queryset = Account.objects.all()
43+
serializer_class = AccountSerializer
44+
45+
def get_permissions(self):
46+
if self.request.method in permissions.SAFE_METHODS:
47+
# only logged in users can see accounts
48+
return (permissions.IsAuthenticated(),)
49+
50+
if self.request.method == 'POST':
51+
return (permissions.AllowAny(),)
52+
53+
return (permissions.IsAuthenticated(), IsAccountOwner(),)
54+
55+
def create(self, request):
56+
serializer = self.serializer_class(data=request.data)
57+
58+
if serializer.is_valid():
59+
if 'password' not in serializer.validated_data:
60+
return Response({
61+
'error': 'Password required for creating account.'
62+
}, status=status.HTTP_400_BAD_REQUEST)
63+
64+
account = Account.objects.\
65+
create_account(**serializer.validated_data)
66+
67+
# add JWT token to response
68+
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
69+
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
70+
71+
payload = jwt_payload_handler(account)
72+
token = jwt_encode_handler(payload)
73+
74+
serializer.validated_data['token'] = token
75+
76+
return Response(serializer.validated_data,
77+
status=status.HTTP_201_CREATED)
78+
79+
return Response({
80+
'error': 'Account could not be created with received data.'
81+
}, status=status.HTTP_400_BAD_REQUEST)
82+
83+
def perform_create(self, serializer):
84+
# Hash password but passwords are not required
85+
if ('password' in self.request.data):
86+
~~ password = make_password(self.request.data['password'])
87+
serializer.save(password=password)
88+
else:
89+
serializer.save()
90+
91+
def perform_update(self, serializer):
92+
# Hash password but passwords are not required
93+
if ('password' in self.request.data):
94+
~~ password = make_password(self.request.data['password'])
95+
serializer.save(password=password)
96+
else:
97+
serializer.save()
98+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
title: django.http Http404 Python Code Examples
2+
category: page
3+
slug: django-http-http404-examples
4+
sortorder: 50045
5+
toc: False
6+
sidebartitle: django.http Http404
7+
meta: Example code for the Http404 Exception class from the django.http module.
8+
9+
10+
[Http404](https://docs.djangoproject.com/en/dev/topics/http/views/#the-http404-exception)
11+
([source code](https://github.com/django/django/blob/master/django/http/response.py))
12+
is a [Django](/django.html) convenience exception class that returns
13+
your application's standard error page and an
14+
[HTTP 404](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) status
15+
code.
16+
17+
Note that while Http404 is typically imported from `django.http`, the
18+
source code for the exception lives under `django.http.responses`.
19+
20+
21+
## Example 1 from wagtail
22+
[wagtail](https://github.com/wagtail/wagtail)
23+
([project website](https://wagtail.io/)) is a
24+
[Django](/django.html)-based content management system (CMS) with
25+
open source code provided under the
26+
[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
27+
28+
[**wagtail / wagtail / core / models.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/core/models.py)
29+
30+
```python
31+
import json
32+
import logging
33+
from collections import defaultdict
34+
from io import StringIO
35+
from urllib.parse import urlparse
36+
37+
from django.conf import settings
38+
from django.contrib.auth.models import Group, Permission
39+
from django.contrib.contenttypes.models import ContentType
40+
from django.core import checks
41+
from django.core.cache import cache
42+
from django.core.exceptions import ValidationError
43+
from django.core.handlers.base import BaseHandler
44+
from django.core.handlers.wsgi import WSGIRequest
45+
from django.db import models, transaction
46+
from django.db.models import Case, Q, Value, When
47+
from django.db.models.functions import Concat, Substr
48+
~~from django.http import Http404
49+
from django.template.response import TemplateResponse
50+
from django.urls import reverse
51+
from django.utils import timezone
52+
from django.utils.functional import cached_property
53+
from django.utils.text import capfirst, slugify
54+
from django.utils.translation import ugettext_lazy as _
55+
56+
## ... code is abbreviated here due to the long file length ...
57+
58+
59+
def route(self, request, path_components):
60+
if path_components:
61+
# request is for a child of this page
62+
child_slug = path_components[0]
63+
remaining_components = path_components[1:]
64+
65+
~~ try:
66+
~~ subpage = self.get_children().get(slug=child_slug)
67+
~~ except Page.DoesNotExist:
68+
~~ raise Http404
69+
70+
return subpage.specific.route(request, remaining_components)
71+
72+
~~ else:
73+
~~ # request is for this very page
74+
~~ if self.live:
75+
~~ return RouteResult(self)
76+
~~ else:
77+
~~ raise Http404
78+
79+
```

content/pages/meta/00-change-log.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ on GitHub.
1919
* Added new [Django](/django-code-examples.html) code examples pages:
2020
* [django.contrib.auth get_user_model](/django-contrib-auth-get-user-model-examples.html)
2121
* [django.contrib.auth.decorators login_required](/django-contrib-auth-decorators-login-required-examples.html)
22+
* [django.contrib.auth.hashers make_password](/django-contrib-auth-hashers-make-password-examples.html)
23+
* [django.http Http404](/django-http-http404-examples.html)
24+
* Updated code examples on the following pages:
25+
* [django.contrib admin](/django-contrib-admin-examples.html)
2226

2327
### July
2428
* Pushed the [3000th commit](https://github.com/mattmakai/fullstackpython.com/commit/180051047a582b9154874b15dccb315600f23300)

theme/templates/table-of-contents.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ <h4 class="bp"><a href="/django-conf-urls-url-examples.html">django.conf.urls.ur
236236
<h4 class="bp"><a href="/django-contrib-admin-examples.html">django.contrib.admin</a></h4>
237237
<h4 class="bp"><a href="/django-contrib-auth-get-user-model-examples.html">django.contrib.auth get_user_model</a></h4>
238238
<h4 class="bp"><a href="/django-contrib-auth-decorators-login-required-examples.html">django.contrib.auth.decorators login_required</a></h4>
239+
<h4 class="bp"><a href="/django-contrib-auth-hashers-make-password-examples.html">django.contrib.auth.hashers make_password</a></h4>
239240
<h4 class="bp"><a href="/django-contrib-admin-filters-simplelistfilter-examples.html">django.contrib.admin.filters SimpleListFilter</a></h4>
240241
<h4 class="bp"><a href="/django-core-mail-send-mail-examples.html">django.core.mail.send_mail</a></h4>
241242
<h4 class="bp"><a href="/django-core-mail-messages-emailmessage-examples.html">django.core.mail.messages EmailMessage</a></h4>
@@ -248,6 +249,7 @@ <h4 class="bp"><a href="/django-db-models-textfield-examples.html">django.db.mod
248249
<h4 class="bp"><a href="/django-db-models-signal-examples.html">django.db.models.signal</a></h4>
249250
<h4 class="bp"><a href="/django-dispatch-dispatcher-signal-examples.html">django.dispatch.dispatcher Signal</a></h4>
250251
<h4 class="bp"><a href="/django-forms-examples.html">django.forms</a></h4>
252+
<h4 class="bp"><a href="/django-http-http404-examples.html">django.http Http404</a></h4>
251253
<h4 class="bp"><a href="/django-urls-path-examples.html">django.urls.path</a></h4>
252254
<h4 class="bp"><a href="/django-utils-html-format-html-examples.html">django.utils.html format_html</a></h4>
253255
<h4 class="bp"><a href="/django-utils-timezone-examples.html">django.utils.timezone</a></h4>

0 commit comments

Comments
 (0)