0
如果用戶輸入錯誤路徑,如何將用戶重定向到我的自定義404.html
頁面?重定向到DjangoCMS中的自定義404 html頁面
views.py:
from django.shortcuts import render, render_to_response, redirect
from django.template import RequestContext
import threading
def url_redirect(request):
return render_to_response('blog.html', RequestContext(request))
def post_redirect(request):
return render_to_response('post.html', RequestContext(request))
def privacy_redirect(request):
return render_to_response('privacy.html', RequestContext(request))
def terms_conditions_redirect(request):
return render_to_response('terms.html', RequestContext(request))
def page_404(request):
return render_to_response('404.html', RequestContext(request))
urls.py:
from __future__ import print_function
from cms.sitemaps import CMSSitemap
from django.conf.urls import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from django.conf import settings
from django.conf.urls import patterns
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^blog', "website.views.url_redirect", name="url-redirect"),
url(r'^post', "website.views.post_redirect", name="post-redirect"),
url(r'^privacy', "website.views.privacy_redirect", name="privacy-redirect"),
url(r'^terms', "website.views.terms_conditions_redirect", name="terms_conditions-redirect"),
url(r'^404', "website.views.page_404", name="page_404"),
url(r'^', include('cms.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns
我也有我的settings.py
文件TEMPLATE_DEBUG = True
和DEBUG = False
。請幫忙嗎?
另外,你們可以告訴我,從views.py
這些方法是好的,如果他們可以改寫更好的方式嗎?
感謝
謝謝。有效。但是URL不會變成'mysite/en/404.html'。相反,它只是在修改後添加路徑。有任何想法嗎 ? –
404s不應該重定向,但只是說,沒有發現任何東西,就是這樣......如果你重定向,你會搞砸你的搜索引擎優化工作...... – benzkji