diff --git a/django_jinja/views/__init__.py b/django_jinja/views/__init__.py index 23c2b8e..1648e77 100644 --- a/django_jinja/views/__init__.py +++ b/django_jinja/views/__init__.py @@ -1,68 +1,34 @@ -from django import http -from django.template import loader -from django.views import View +from django.views import defaults from ..base import get_match_extension -class GenericView(View): - response_cls = http.HttpResponse - content_type = "text/html" - tmpl_name = None +def bad_request(request, exception=None): + return defaults.bad_request( + request, + exception, + template_name=f"400{get_match_extension() or '.jinja'}" + ) - def get_context_data(self): - return {"view": self} - def get(self, request, *args, **kwargs): - context = self.get_context_data() - template_name = callable(self.tmpl_name) and self.tmpl_name() or self.tmpl_name - output = loader.render_to_string(template_name, context, request=request) - return self.response_cls(output, content_type=self.content_type) +def permission_denied(request, exception=None): + return defaults.permission_denied( + request, + exception, + template_name=f"403{get_match_extension() or '.jinja'}" + ) -class ErrorView(GenericView): - def head(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) +def page_not_found(request, exception=None): + return defaults.page_not_found( + request, + exception, + template_name=f"404{get_match_extension() or '.jinja'}" + ) - def post(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - def options(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - def delete(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - def put(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - def patch(self, request, *args, **kwargs): - return self.get(request, *args, **kwargs) - - -class PageNotFound(ErrorView): - response_cls = http.HttpResponseNotFound - - def tmpl_name(self): - return "404" + (get_match_extension() or ".jinja") - - -class PermissionDenied(ErrorView): - response_cls = http.HttpResponseForbidden - - def tmpl_name(self): - return "403" + (get_match_extension() or ".jinja") - - -class BadRequest(ErrorView): - response_cls = http.HttpResponseBadRequest - - def tmpl_name(self): - return "400" + (get_match_extension() or ".jinja") - - -class ServerError(ErrorView): - response_cls = http.HttpResponseServerError - - def tmpl_name(self): - return "500" + (get_match_extension() or ".jinja") +def server_error(request): + return defaults.server_error( + request, + template_name=f"500{get_match_extension() or '.jinja'}" + ) diff --git a/doc/content.adoc b/doc/content.adoc index 7fe0684..fe024eb 100644 --- a/doc/content.adoc +++ b/doc/content.adoc @@ -548,29 +548,22 @@ library.extension(MyExtension) This only works within a Django app. If you don't have an app for your project, create an app specifically for this purpose and put your templatetags there. -=== Render 4xx/500 pages with jinja +=== Render error pages with jinja -django-jinja also provides a set of views for easy -render 4xx/500 pages using jinja engine: +django-jinja also provides a set of views for rendering +400, 403, 404, and 500 error pages using the jinja engine: [source, python] ---- # yourproject/urls.py from django_jinja import views -handler400 = views.BadRequest.as_view() -handler403 = views.PermissionDenied.as_view() -handler404 = views.PageNotFound.as_view() -handler500 = views.ServerError.as_view() +handler400 = views.bad_request +handler403 = views.permission_denied +handler404 = views.page_not_found +handler500 = views.server_error ---- - -== Known Issues - -- The above handler500 is broken as of django 2.2. see: https:github.com/niwinz/django-jinja/issues/234 - - - == Builtin contrib modules *django-jinja* comes with some additional contrib modules that adapt a limited set of external diff --git a/testing/testapp/urls.py b/testing/testapp/urls.py index 1a17411..238e745 100644 --- a/testing/testapp/urls.py +++ b/testing/testapp/urls.py @@ -13,9 +13,9 @@ url(r"^test1/(?P\d+)/$", BasicTestView.as_view(), name="test-1"), url(r"^test-i18n/$", I18nTestView.as_view(), name="i18n-test"), url(r"^test-i18n-dtl/$", I18nTestViewDTL.as_view(), name="i18n-dtl-test"), - url(r"^test/404$", views.PageNotFound.as_view(), name="page-404"), - url(r"^test/403$", views.PermissionDenied.as_view(), name="page-403"), - url(r"^test/500$", views.ServerError.as_view(), name="page-500"), + url(r"^test/404$", views.page_not_found, name="page-404"), + url(r"^test/403$", views.permission_denied, name="page-403"), + url(r"^test/500$", views.server_error, name="page-500"), url(r"^test-streaming/$", StreamingTestView.as_view(), name='streaming-test'), url(r"^testmodel/$", ListTestView.as_view()),