django POST返回内部服务器500错误

Man*_*pta 0 python django ajax

我正在尝试将请求发送到django视图,但它一直在返回INTERNAL SERVER ERROR 500.

我的ajax帖子:

$.ajax({
    url : "/loginAction/",
    type : "POST",
    async : false,
    data : {action:'loginAction',
            email:email,
            password:password},

    success : function(response) {
        $.niftyNoty({
            type:"success",icon:"",title:"Login Successful. Redirecting....",container:"floating",timer:5000
        });
    },

    error : function(xhr,errmsg,err) {
        console.log(xhr.status + ": " + xhr.responseText);
        $.niftyNoty({
            type:"danger",icon:"",title:"Wrong Email OR Password",container:"floating",timer:5000
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

我的django观点:

def loginAction(request):
    print "Its workjing"
    if request.method == 'POST' and 'loginButton' in request.POST:
        email = request.POST.get('email')
        password = request.POST.get('password')

        print email, password

        return HttpResponse(json.dumps({}),content_type="application/json")
Run Code Online (Sandbox Code Playgroud)

我的urls.py

urlpatterns = [
            url(r'^', views.loginPage, name='loginPage'),
            url(r'^loginAction/', views.loginAction, name='loginAction')
        ]
Run Code Online (Sandbox Code Playgroud)

ajax POST没有点击django视图.它不是Its working在控制台中打印.所以它不会将json响应返回给ajax调用.我也试过普通的表格POST,但结果相同.我正在使用django 1.9.2.我无法弄清楚为什么会出现这个错误?

它返回此错误代码:

Internal Server Error: /loginAction/
Traceback (most recent call last):
  File "/home/manish/Desktop/admin_picknbox/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 158, in get_response
    % (callback.__module__, view_name))
ValueError: The view login_app.views.loginPage didn't return an HttpResponse object. It returned None instead.
Run Code Online (Sandbox Code Playgroud)

编辑:ajax标题:

jQuery(document).ready(function($){
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

dor*_*oru 5

看起来你的网址是问题所在,因为在出​​现错误时会看到loginPage视图被调用/loginAction/.所以尝试$在每个正则表达式的末尾添加如下:

urlpatterns = [
        url(r'^$', views.loginPage, name='loginPage'),
        url(r'^loginAction/$', views.loginAction, name='loginAction')
    ]
Run Code Online (Sandbox Code Playgroud)

因为您的第一个正则表达式似乎r'^'捕获了任何网址.