简体   繁体   English

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

[英]django POST returning Internal Server 500 error

I am trying to POST a request to a django view but it keeps returning INTERNAL SERVER ERROR 500 . 我正在尝试将请求发布到django视图,但它一直返回INTERNAL SERVER ERROR 500

My ajax post: 我的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
        });
    }
});

My django view: 我的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")

My urls.py 我的urls.py

urlpatterns = [
            url(r'^', views.loginPage, name='loginPage'),
            url(r'^loginAction/', views.loginAction, name='loginAction')
        ]

The ajax POST is not hitting the django view. Ajax POST没有进入django视图。 It is not printing Its working in console. 它没有打印在控制台中Its working So its not returning the json response to ajax call. 因此,它没有将json响应返回给ajax调用。 I also tried normal form POST but same result. 我也尝试了普通形式的POST,但结果相同。 I am using django 1.9.2. 我正在使用Django 1.9.2。 I cant figure out why this error? 我不知道为什么这个错误?

It returns this error code: 它返回以下错误代码:

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.

EDIT: ajax Header: 编辑:ajax标头:

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

It seems your urls are the problem because in the error appears that the loginPage view is called although you go to /loginAction/ . 看来您的网址是问题所在,因为尽管您转到/loginAction/但在错误中似乎调用了loginPage视图。 So try to add $ at the end of each regex as below: 因此,尝试在每个正则表达式的末尾添加$ ,如下所示:

urlpatterns = [
        url(r'^$', views.loginPage, name='loginPage'),
        url(r'^loginAction/$', views.loginAction, name='loginAction')
    ]

Because it appears that your first regex r'^' captures any url. 因为您的第一个正则表达式r'^'似乎捕获了所有URL。

Your view function doesn't take care of all cases, in case if request.method == 'POST' and 'loginButton' in request.POST: is False , you view function doesn't return anything, hence the error. 您的视图函数不会处理所有情况,以防万一, if request.method == 'POST' and 'loginButton' in request.POST:False ,则您的视图函数不会返回任何内容,因此会出现错误。 Python function, if left without explicit return statement, would return None . 如果没有显式的return语句,Python函数将返回None

Edit: 编辑:

If your print statement is not even executed, then you must have 403 response from django. 如果您的print语句甚至没有执行,那么您必须收到来自django的403响应。 You need to pass csrf token when you make ajax call to prevent attack from unknown person. 进行ajax调用时,您需要传递csrf令牌,以防止来自未知人员的攻击。 Django would check csrf automatically, but you need to pass it as part of the data: Django会自动检查csrf ,但您需要将其作为数据的一部分传递:

data : {action:'loginAction',
        email:email,
        password:password,
        csrfmiddlewaretoken: '{{ csrf_token }}'},

Also, you should check "action" in request.POST not "loginAction" in request.POST . 另外,您应该"action" in request.POST检查"action" in request.POST而不是"action" in request.POST "loginAction" in request.POST

i got this error one day but i latter realize that the page returning internal server error has the same name as the html page i wanted to return. 我有一天收到此错误,但后来我意识到返回内部服务器错误的页面与我要返回的html页面同名。 i just changed the name of the html page i wanted t return and everything worked fine 我只是更改了我不想返回的html页面的名称,并且一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM