为什么1是好的,但2是错误,使用django和jquery

时间:2010-01-20 05:59:54

标签: python django

django观点:

def json_view(request):
    import json
    tag=request.GET.get('tag')
    if tag=='userName':
        username=request.GET.get('userName')
        result='successName'
        if username:
            try:
                user=User.objects.get(username=username)
                result='existName'
            except User.DoesNotExist:
                pass
            return HttpResponse(json.dumps({'result': result}))

    if tag=='email':
        email=request.GET.get('email')
        result='emailSuccess'
        if email:
            try:
                user=User.objects.get(email=email)
                result='notOnly'
            except User.DoesNotExist:
                pass
            return HttpResponse(json.dumps({'result': result}))

1:

$.getJSON('/json_view/', {
                                tag: "userName",
                                userName: G
                            },
                            function (H) {
                                if (H.result == "successName") {
                                    F.showOk(h.ok);
                                    d = true
                                } else {
                                    if (H.result == "existName") {
                                        F.showErr(h.userNameExist);
                                        d = false
                                    }
                                }
                            })

2:

$.getJSON('/json_view/', {
                    tag: "email",
                    email: F
                },


                function (G) {
                    if (G.result == "emailSuccess") {
                        E.showOk(h.ok);
                        B = true
                    } else {
                        if (G.result == "notOnly") {
                            E.showErr(h.emailExist);
                            B = false
                        }
                    }
                })

为什么?

感谢

错误是:

GET http://127.0.0.1:8000/json_view/?tag=email&email=zjm1126%40qq.com 500 INTERNAL SERVER ERROR

1 个答案:

答案 0 :(得分:1)

编写隔离问题的单元测试。文档:http://docs.djangoproject.com/en/dev/topics/testing/

测试的主体应该是这样的:

from django.test.client import Client
c = Client()
response = c.get('/json_view/', {'tag': 'email', 'email': '...'})