本文介绍了为什么1可以,但是2是错误,使用django和jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


推荐答案

p>编写隔离问题的单元测试。文档:

Write a unit test that isolates the problem. Documentation: http://docs.djangoproject.com/en/dev/topics/testing/

测试的正文应该是这样的:

The body of the test should be something like:

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

这篇关于为什么1可以,但是2是错误,使用django和jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:39