Skip to content

Commit

Permalink
Merge pull request liangliangyy#79 from liangliangyy/dev
Browse files Browse the repository at this point in the history
修改错误页面http状态码
  • Loading branch information
liangliangyy authored Feb 4, 2018
2 parents 72a71c1 + 62472d7 commit db515a4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DjangoBlog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

handler404 = 'blog.views.page_not_found_view'
handler500 = 'blog.views.server_error_view'

handle403 = 'blog.views.permission_denied_view'
urlpatterns = [
url(r'^admin/', admin_site.urls),
url(r'', include('blog.urls', namespace='blog')),
Expand Down
18 changes: 14 additions & 4 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def test_validate_article(self):
s = load_articletags(article)
self.assertIsNotNone(s)

rsp = self.client.get('/refresh')
self.assertEqual(rsp.status_code, 302)

self.client.login(username='liangliangyy', password='liangliangyy')
rsp = self.client.get('/refresh')
self.assertEqual(rsp.status_code, 200)

response = self.client.get(reverse('blog:archives'))
self.assertEqual(response.status_code, 200)

Expand All @@ -107,8 +114,7 @@ def test_validate_article(self):
self.client.login(username='liangliangyy', password='liangliangyy')
from DjangoBlog.spider_notify import SpiderNotify
SpiderNotify.baidu_notify([article.get_full_url()])
rsp = self.client.get('/refresh/')
self.assertEqual(rsp.status_code, 200)

from blog.templatetags.blog_tags import gravatar_url, gravatar
u = gravatar_url('[email protected]')
u = gravatar('[email protected]')
Expand All @@ -132,6 +138,10 @@ def test_validate_feed(self):
user = BlogUser.objects.get_or_create(email="[email protected]", username="liangliangyy")[0]
user.set_password("liangliangyy")
user.save()
self.client.login(username='liangliangyy', password='liangliangyy')

rsp = self.client.get('/refresh')
self.assertEqual(rsp.status_code, 403)

response = self.client.get('/feed/')
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -159,5 +169,5 @@ def test_image(self):
"""

def test_errorpage(self):
self.client.get('/eee')
self.client.get('/refresh_memcache')
rsp = self.client.get('/eee')
self.assertEqual(rsp.status_code, 404)
16 changes: 7 additions & 9 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,21 @@ def refresh_memcache(request):
return HttpResponse(e)


def page_not_found_view(request, exception):
def page_not_found_view(request, exception, template_name='blog/error_page.html'):
if exception:
logger.error(exception)
url = request.get_full_path()
return render(request, 'blog/error_page.html',
{'message': '哎呀,您访问的地址 ' + url + ' 是一个未知的地方。请点击首页看看别的?', 'statuscode': '404'})
return render(request, template_name,
{'message': '哎呀,您访问的地址 ' + url + ' 是一个未知的地方。请点击首页看看别的?', 'statuscode': '404'}, status=404)


def server_error_view(request, exception):
if exception:
logger.error(exception)
return render(request, 'blog/error_page.html',
def server_error_view(request, template_name='blog/error_page.html'):
return render(request, template_name,
{'message': '哎呀,出错了,我已经收集到了错误信息,之后会抓紧抢修,请点击首页看看别的?', 'statuscode': '500'})


def permission_denied_view(request, exception):
def permission_denied_view(request, exception, template_name='blog/error_page.html'):
if exception:
logger.error(exception)
return render(request, 'blog/error_page.html',
return render(request, template_name,
{'message': '哎呀,您没有权限访问此页面,请点击首页看看别的?', 'statuscode': '403'})
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ appdirs==1.4.3
bottle==0.12.13
certifi==2018.1.18
chardet==3.0.4
coverage==4.4.2
Django==2.0.1
coverage==4.5
Django==2.0.2
django-appconf==1.0.2
django-autoslug==1.9.3
django-compressor==2.2
Expand Down
4 changes: 2 additions & 2 deletions travis_test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ appdirs==1.4.3
bottle==0.12.13
certifi==2018.1.18
chardet==3.0.4
coverage==4.4.2
Django==2.0.1
coverage==4.5
Django==2.0.2
django-appconf==1.0.2
django-autoslug==1.9.3
django-compressor==2.2
Expand Down

0 comments on commit db515a4

Please sign in to comment.