Skip to content

Commit d5b5ace

Browse files
committed
add new login_required example
1 parent 8454935 commit d5b5ace

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ open source under the
151151
* [django.db.models CharField](/django-db-models-charfield-examples.html)
152152
* [django.db.models DateTimeField](/django-db-models-datetimefield-examples.html)
153153

154+
154155
### django-oscar
155156
[django-oscar](https://github.com/django-oscar/django-oscar/)
156157
([project website](http://oscarcommerce.com/))
@@ -162,6 +163,7 @@ source under a
162163
Further code examples from django-oscar:
163164

164165
* [django.contrib.admin](/django-contrib-admin-examples.html)
166+
* [django.contrib.auth.decorators login_required](/django-contrib-auth-decorators-login-required-examples.html)
165167

166168

167169
### django-smithy

content/pages/examples/django/django-contrib-auth-decorators-login-required.markdown

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ boilerplate like `if not request.user.is_authenticated:`.
2121
web app built in [Django](/django.html). The code is open source under the
2222
[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
2323

24+
The dccnsys project provides a typical `login_required` decorator usage
25+
example. The decorator is placed on a view function, in this case `personal`,
26+
to protect it from being accessible by unauthenticated users.
27+
2428
[**dccnsys / wwwdccn / registration / views.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/registration/views.py)
2529

2630
```python
@@ -83,3 +87,80 @@ def subscriptions(request):
8387
'form': form
8488
})
8589
```
90+
91+
## Example 2 from django-oscar
92+
[django-oscar](https://github.com/django-oscar/django-oscar/)
93+
([project website](http://oscarcommerce.com/))
94+
is a framework for building e-commerce sites on top of
95+
[Django](/django.html). The code for the project is available open
96+
source under a
97+
[custom license written by Tangent Communications PLC](https://github.com/django-oscar/django-oscar/blob/master/LICENSE).
98+
99+
The following `login_required` example is one that surprised me
100+
a bit because I had not previously seen `login_required` applied
101+
on the parameter to the [url](/django-conf-urls-url-examples.html)
102+
function. Typically the decorator is used on view function where
103+
it is defined, but this way works as well as long as you are
104+
consistent about where you apply the decorator in your code base.
105+
106+
[**django-oscar / src / oscar / apps / customer / apps.py**](https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/customer/apps.py)
107+
108+
```python
109+
from django.conf.urls import url
110+
~~from django.contrib.auth.decorators import login_required
111+
from django.utils.translation import gettext_lazy as _
112+
from django.views import generic
113+
114+
from oscar.core.application import OscarConfig
115+
from oscar.core.loading import get_class
116+
117+
118+
class CustomerConfig(OscarConfig):
119+
label = 'customer'
120+
name = 'oscar.apps.customer'
121+
verbose_name = _('Customer')
122+
123+
namespace = 'customer'
124+
125+
def ready(self):
126+
from . import receivers # noqa
127+
from .alerts import receivers # noqa
128+
129+
self.summary_view = get_class('customer.views',
130+
'AccountSummaryView')
131+
self.order_history_view = get_class('customer.views',
132+
'OrderHistoryView')
133+
self.order_detail_view = get_class('customer.views',
134+
'OrderDetailView')
135+
136+
## ... abbreviating code not relevant to login_required ...
137+
138+
139+
def get_urls(self):
140+
urls = [
141+
# Login, logout and register doesn't require login
142+
url(r'^login/$', self.login_view.as_view(),
143+
name='login'),
144+
url(r'^logout/$', self.logout_view.as_view(),
145+
name='logout'),
146+
url(r'^register/$', self.register_view.as_view(),
147+
name='register'),
148+
~~ url(r'^$', login_required(self.summary_view.as_view()),
149+
~~ name='summary'),
150+
~~ url(r'^change-password/$',
151+
~~ login_required(self.change_password_view.as_view()),
152+
~~ name='change-password'),
153+
154+
# Profile
155+
~~ url(r'^profile/$',
156+
~~ login_required(self.profile_view.as_view()),
157+
~~ name='profile-view'),
158+
~~ url(r'^profile/edit/$',
159+
~~ login_required(self.profile_update_view.as_view()),
160+
~~ name='profile-update'),
161+
~~ url(r'^profile/delete/$',
162+
~~ login_required(self.profile_delete_view.as_view()),
163+
~~ name='profile-delete'),
164+
165+
## the file continues with further examples that show the same usage
166+
```

0 commit comments

Comments
 (0)