统一使用 error_page 的形式返回错误页面

This commit is contained in:
virusdefender 2015-08-14 10:22:32 +08:00
parent e84039eea1
commit 11a4c04443
3 changed files with 12 additions and 5 deletions

View File

@ -4,7 +4,7 @@ from rest_framework.views import APIView
from django.shortcuts import render
from utils.shortcuts import serializer_invalid_response, error_response, success_response
from utils.shortcuts import paginate
from utils.shortcuts import paginate, error_page
from .models import Announcement
from .serializers import (CreateAnnouncementSerializer, AnnouncementSerializer,
EditAnnouncementSerializer)
@ -14,7 +14,7 @@ def announcement_page(request, announcement_id):
try:
announcement = Announcement.objects.get(id=announcement_id, visible=True)
except Announcement.DoesNotExist:
return render(request, "utils/error.html", {"error": u"模板不存在"})
return error_page(request, u"模板不存在")
return render(request, "oj/announcement/announcement.html", {"announcement": announcement})

View File

@ -12,7 +12,8 @@ from rest_framework.views import APIView
from django.conf import settings
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate, rand_str
from utils.shortcuts import (serializer_invalid_response, error_response,
success_response, paginate, rand_str, error_page)
from .serizalizers import (CreateProblemSerializer, EditProblemSerializer, ProblemSerializer,
ProblemTagSerializer, CreateProblemTagSerializer)
from .models import Problem, ProblemTag
@ -43,7 +44,7 @@ def problem_page(request, problem_id):
try:
problem = Problem.objects.get(id=problem_id)
except Problem.DoesNotExist:
return render(request, "utils/error.html", {"error": u"题目不存在"})
return error_page(request, u"题目不存在")
return render(request, "oj/problem/problem.html", {"problem": problem, "samples": json.loads(problem.samples)})

View File

@ -3,17 +3,23 @@ import hashlib
import time
import random
from django.shortcuts import render
from django.core.paginator import Paginator
from rest_framework.response import Response
def error_page(request, error_reason):
return render(request, "utils/error.html", {"error": error_reason})
def error_response(error_reason):
return Response(data={"code": 1, "data": error_reason})
def serializer_invalid_response(serializer):
return error_response(serializer.errors)
for k, v in serializer.errors.iteritems():
return error_response(k + " : " + v[0])
def success_response(data):