@@ -222,13 +222,32 @@ These information is obtained using [user-agents](https://github.com/selwin/pyth
222
222
223
223
### Access Mixins
224
224
225
+ #### AllowContentTypeMixin
226
+
227
+ Restrict the content type of http request.
228
+
229
+ ``` py
230
+ from django.views.generic import TemplateView
231
+ from django_boost.views.mixins import AllowContentTypeMixin
232
+
233
+ class PostView (AllowContentTypeMixin , TemplateView ):
234
+ allowed_content_types = [" application/xml" ]
235
+ template_name = " path/to/template"
236
+
237
+ ```
238
+ Restrict request based on ` Content-Type ` of http header.
239
+
240
+ If the content type is not allowed, http415 response will be returned.
241
+ You can disable restrictions by specifying ` strictly = False `
242
+
243
+
225
244
#### ReAuthenticationRequiredMixin
226
245
227
246
``` py
228
247
from django.views.generic import TemplateView
229
248
from django_boost.views.mixins import ReAuthenticationRequiredMixin
230
249
231
- class RecentLogin (ReAuthenticationRequiredMixin ,TemplateView ):
250
+ class RecentLogin (ReAuthenticationRequiredMixin , TemplateView ):
232
251
template_name = " mypage.html"
233
252
auth_unnecessary = 3600
234
253
```
@@ -294,17 +313,68 @@ You can change the query string parameter name by changing `redirect_field_name`
294
313
295
314
#### UserAgentMixin
296
315
``` py
297
- from django .views.generic import TemplateView
316
+ from django_boost .views.generic import TemplateView
298
317
from django_boost.views.mixins import UserAgentMixin
299
318
300
- class SameView (UserAgentMixin ,TemplateView ):
319
+ class SameView (UserAgentMixin , TemplateView ):
320
+ template_name = " default_template"
301
321
pc_template_name = " pc_template.html"
302
322
tablet_template_name = " tablet_template.html"
303
323
mobile_template_name = " mobile_template.html"
304
324
```
305
325
306
- Switch the template file to be displayed by user agent.
326
+ Assign ` user_agent ` attribute to` self.request ` and
327
+ switch the template file to be displayed by user agent.
328
+
329
+ If the user agent can not be determined, the template specified in ` template_name ` will be used.
330
+ ` pc_template_name ` ,` tablet_template_name ` ,` mobile_template_name ` has no arms, but` template_name ` is required.
331
+
332
+ #### JsonRequestMixin
333
+ A specialized mixin for ` AllowContentTypeMixin ` for json.
334
+
335
+ ``` py
336
+ from django.views.generic import TemplateView
337
+ from django_boost.views.mixins import JsonRequestMixin
338
+
339
+ class PostView (JsonRequestMixin , TemplateView ):
340
+ template_name = " path/to/template"
341
+
342
+ def get_context_data (self ,** kwargs ):
343
+ posted_data = self .json
344
+ # {"send" : "from cliant"}
345
+ return posted_data
346
+ ```
347
+
348
+ You can access the dictionary object parsed from the Json string sent by the client in ` self.json `
349
+
350
+ If you use for the purpose of API ` JsonView ` below is recommended.
351
+
352
+
353
+ ### ResponseMixin
354
+
355
+ #### JsonResponseMixin
356
+ Returns the response in Json format
357
+
358
+ ``` py
359
+ from django.views.generic import TemplateView
360
+ from django_boost.views.mixins import JsonResponseMixin
361
+
362
+ class JsonResponseView (JsonResponseMixin , TemplateView ):
363
+ extra_context = {" context" : " ..." }
364
+
365
+ def get_context_data (self ,** kwargs ):
366
+ context = {}
367
+ context.update(super ().get_context_data(** kwargs))
368
+ return context
369
+
370
+ ```
371
+ The usage of ` extra_context ` and` get_context_data ` is basically the same as ` TemplateView ` .
372
+ The difference is that ` TemplateView ` is passed directly to the template context, whereas` JsonResponseMixin ` is a direct response.
373
+
307
374
375
+ Specify ` strictly = True ` if you want to limit the Content-Type to Json only.
376
+
377
+ If you use for the purpose of API ` JsonView ` below is recommended.
308
378
309
379
### Form Mixin
310
380
@@ -341,16 +411,58 @@ class CustomerSearchView(FormView):
341
411
342
412
### GenericView
343
413
414
+ #### Extended Views
415
+
416
+ ``` py
417
+ from django_boost.views.generic import View
418
+
419
+ class YourView (View ):
420
+
421
+ def setup (self , request , * args , ** kwargs ):
422
+ super ().setup(request, * args, ** kwargs)
423
+ # # some process before view process
424
+
425
+ # # For example, add attribute to view class
426
+
427
+ def after_view_process (self , request , response , * args , ** kwargs ):
428
+ super ().after_view_process(request, response, * args, ** kwargs)
429
+ # # some process after view process
430
+
431
+ # # For example, add http headers to the response
432
+
433
+ return response
434
+
435
+ ```
436
+ django_boost generic view (
437
+ ` CreateView ` , ` DeleteView ` , ` DetailView ` , ` FormView ` , ` ListView ` , ` TemplateView ` , ` UpdateView ` , ` View ` ) classes has ` setup ` and ` after_view_process ` method, These are called before and after processing of View respectively. ` setup ` method is same as the method added in Django 2.2 .
438
+
439
+ #### JsonView
440
+ ` JsonResponseMixin ` と` JsonRequestMixin ` を継承したgeneric view class です。
441
+ ``` py
442
+ from django_boost.views.generic import JsonView
443
+
444
+ class SameAPIView (JsonView ):
445
+
446
+ def get_context_data (self ,** kwargs ):
447
+ return self .json
448
+ ```
449
+
450
+ In the above example, we just return the sent Json string as it is.
451
+
452
+
344
453
#### ModelCRUDViews
345
454
346
455
Provides easy creation of CRUDViews linked to model.
456
+
457
+ ` views.py `
347
458
``` py
348
459
from django_boost.views.generic import ModelCRUDViews
349
460
350
461
class CustomerViews (ModelCRUDViews ):
351
462
model = Customer
352
463
```
353
464
465
+ ` urls.py `
354
466
``` py
355
467
from django.urls import path, include
356
468
from . import views
@@ -359,6 +471,38 @@ urlpatterns = [
359
471
path(' views/' ,include(views.CustomerViews().urls)),
360
472
]
361
473
```
474
+ In the template you can use as follows.
475
+
476
+ ``` html+django
477
+ {% url 'customer:list' %}
478
+ {% url 'customer:create' %}
479
+ {% url 'customer:detail' %}
480
+ {% url 'customer:update' %}
481
+ {% url 'customer:delete' %}
482
+ ```
483
+ The name of the URL is defined under the namespace of the lower-cased model class name.
484
+
485
+ ###### Case of Namespaced
486
+ ` urls.py `
487
+ ``` py
488
+ from django.urls import path, include
489
+ from . import views
490
+
491
+ app_name = " myapp"
492
+ urlpatterns = [
493
+ path(' views/' ,include(views.CustomerViews(app_name = " myapp:customer" ).urls)),
494
+ ]
495
+
496
+ ```
497
+
498
+ In the template you can use as follows.
499
+ ``` html+django
500
+ {% url 'myapp:customer:list' %}
501
+ {% url 'myapp:customer:create' %}
502
+ {% url 'myapp:customer:detail' %}
503
+ {% url 'myapp:customer:update' %}
504
+ {% url 'myapp:customer:delete' %}
505
+ ```
362
506
363
507
### Template Tags
364
508
@@ -424,7 +568,68 @@ Replace the query string of the current page URL with the argument.
424
568
{# case of current page's query string is `?id=2`#}
425
569
{% replace_parameters request 'id' 1 'age' 20 %}
426
570
427
- {# The result of replacing is `?id=2&age=20` #}
571
+ {# The result of replacing is `?id=1&age=20` #}
572
+
573
+ ```
574
+ Useful for pagination.
575
+
576
+ ## utilty functions
577
+
578
+ ### loop utils
579
+
580
+ #### loopfirst
581
+
582
+ Yield True when the first element of the given iterator object, False otherwise.
583
+
584
+ ``` py
585
+ from django_boost.utils.functions import loopfirst
586
+
587
+
588
+ for is_first, v in loopfirst(range (5 )):
589
+ print (is_first, v)
590
+
591
+ # True 0
592
+ # False 1
593
+ # False 2
594
+ # False 3
595
+ # False 4
596
+ ```
597
+
598
+
599
+ #### looplast
600
+
601
+ Yield True when the last element of the given iterator object, False otherwise.
602
+
603
+ ``` py
604
+ from django_boost.utils.functions import looplast
605
+
606
+
607
+ for is_last, v in looplast(range (5 )):
608
+ print (is_last, v)
609
+
610
+ # False 0
611
+ # False 1
612
+ # False 2
613
+ # False 3
614
+ # True 4
615
+ ```
616
+
617
+ #### loopfirstlast
618
+
619
+ A function combining ` firstloop ` and` lastloop ` .
620
+
621
+ Yield True if the first and last element of the iterator object, False otherwise.
622
+
623
+ ``` py
624
+ from django_boost.utils.functions import loopfirstlast
625
+
626
+
627
+ for first_or_last, v in loopfirstlast(range (5 )):
628
+ print (first_or_last, v)
428
629
630
+ # True 0
631
+ # False 1
632
+ # False 2
633
+ # False 3
634
+ # True 4
429
635
```
430
- Useful for pagination.
0 commit comments