public: add custom handler for 404 pages (#8681)
This commit is contained in:
parent
413c2024f0
commit
c7911492a4
|
@ -0,0 +1,14 @@
|
|||
{% extends "combo/page_template.html" %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block combo-content %}
|
||||
<div>
|
||||
<h2>{% trans "This page doesn't exist" %}</h2>
|
||||
{% blocktrans %}
|
||||
<p>
|
||||
Maybe it got removed, maybe a link is wrong, luckily there's still
|
||||
the <a href="/">home page</a> to get back to a safe place.
|
||||
</p>
|
||||
{% endblocktrans %}
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -186,13 +186,16 @@ def page(request):
|
|||
return HttpResponsePermanentRedirect(url + '/')
|
||||
raise Http404()
|
||||
|
||||
pages = list(page.get_parents_and_self())
|
||||
|
||||
if page.get_online_url() != url:
|
||||
if not url.endswith('/') and settings.APPEND_SLASH:
|
||||
return HttpResponsePermanentRedirect(url + '/')
|
||||
raise Http404()
|
||||
|
||||
return publish_page(request, page)
|
||||
|
||||
def publish_page(request, page, status=200):
|
||||
pages = list(page.get_parents_and_self())
|
||||
|
||||
if not page.is_visible(request.user):
|
||||
if not request.user.is_authenticated():
|
||||
from django.contrib.auth.views import redirect_to_login
|
||||
|
@ -217,4 +220,12 @@ def page(request):
|
|||
}
|
||||
|
||||
template_name = combo_template['template']
|
||||
return render(request, template_name, ctx)
|
||||
return render(request, template_name, ctx, status=status)
|
||||
|
||||
def error404(request, *args, **kwargs):
|
||||
page = Page()
|
||||
page.template_name = '404'
|
||||
if not '404' in settings.COMBO_PUBLIC_TEMPLATES:
|
||||
settings.COMBO_PUBLIC_TEMPLATES['404'] = settings.COMBO_PUBLIC_TEMPLATES['standard'].copy()
|
||||
settings.COMBO_PUBLIC_TEMPLATES['404']['template'] = 'combo/404.html'
|
||||
return publish_page(request, page, status=404)
|
||||
|
|
|
@ -20,7 +20,7 @@ from django.contrib import admin
|
|||
|
||||
from .urls_utils import decorated_includes, manager_required
|
||||
|
||||
from .public.views import login, logout
|
||||
from .public.views import login, logout, error404
|
||||
from .manager.urls import urlpatterns as combo_manager_urls
|
||||
|
||||
from . import plugins
|
||||
|
@ -31,8 +31,11 @@ urlpatterns = patterns('',
|
|||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'^logout/$', logout, name='auth_logout'),
|
||||
url(r'^login/$', login, name='auth_login'),
|
||||
url(r'^404$', error404),
|
||||
)
|
||||
|
||||
handler404 = error404
|
||||
|
||||
if 'mellon' in settings.INSTALLED_APPS:
|
||||
urlpatterns += patterns('', url(r'^accounts/mellon/', include('mellon.urls')))
|
||||
|
||||
|
|
|
@ -157,3 +157,9 @@ def test_subpage_location():
|
|||
|
||||
resp = app.get('/second/child-second/grand-child-second/', status=200)
|
||||
assert 'Grand child of second' in resp.body
|
||||
|
||||
def test_404():
|
||||
Page.objects.all().delete()
|
||||
app = TestApp(application)
|
||||
resp = app.get('/foobar/', status=404)
|
||||
assert "This page doesn't exist" in resp.body
|
||||
|
|
Loading…
Reference in New Issue