Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the error views. #315

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 24 additions & 58 deletions django_jinja/views/__init__.py
Original file line number Diff line number Diff line change
@@ -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'}"
)
21 changes: 7 additions & 14 deletions doc/content.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions testing/testapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
url(r"^test1/(?P<data>\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()),
Expand Down