Manish Gupta February 2016

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.

My ajax post:

$.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:

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

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

The ajax POST is not hitting the django view. It is not printing Its working in console. So its not returning the json response to ajax call. I also tried normal form POST but same result. I am using 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.
        

Answers


Shang Wang February 2016

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. Python function, if left without explicit return statement, would return None.

Edit:

If your print statement is not even executed, then you must have 403 response from django. You need to pass csrf token when you make ajax call to prevent attack from unknown person. Django would check csrf automatically, but you need to pass it as part of the data:

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

Also, you should check "action" in request.POST not "loginAction" in request.POST.


doru February 2016

It seems your urls are the problem because in the error appears that the loginPage view is called although you go to /loginAction/. 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.

Post Status

Asked in February 2016
Viewed 3,288 times
Voted 7
Answered 2 times

Search




Leave an answer