HTTP 500 Error with Django Ajax
Question: I'm trying to use Django with AJAX calls, and it's giving me HTTP 500 even of
MultiValueDictKeyError even though there's nothing wrong?

I sent 3 variables: sendEmail, username, error

I was able to use a request.POST on the 3 variables, and get the following
output:

* * *

sendEmail = True

username = someUserName

error = Login

* * *

However, a webpage is returned with an HTTP 500

View.py:

    
    
    def loginUser(request):
        username = ""
        type = ""
        logger = logging.getLogger('views.logger.login')
    
        try:
            username = request.POST['username'];
            logger.info("User:" + username + " in Login Page")
        except MultiValueDictKeyError:
            logger.info("Cannot Identify User")
    
        try:
            type = request.POST['submit']
            logger.info("User:" + username + " requests:" + type)
        except MultiValueDictKeyError:
            logger.info("Cannot Identify User's Request")
    
        if(type=="Login"):
            try:
                username = request.POST['username']
                password = request.POST['password']
    
                logger.info("UserName:" + username + " is trying to login")
    
                user = authenticate(username=username, password=password)
                if user is not None:
                    if user.is_active:
                        login(request, user)
                        logger.info("User is active, and logged in")
                        return redirect('index.html')
                    else:
                        logger.info("User is not active, and will not be logged in")
                        return redirect('disabled.html')
                else:
                    logger.info("User:" + username + " is not valid");
                    context = {'Status': "Please sign in", 'Error': "Invalid", 'username':username}
                    return render(request, 'webapp/login.html', context)
            except MultiValueDictKeyError:
                logger.info("The user have missing forms")
                context = {'Status': "Please sign in", 'Error': "Null"}
                return render(request, 'webapp/login.html', context)
            except Exception as e:
                context = {'Status': "Please sign in", 'Error': "Null"}
                return render(request, 'webapp/register.html', context)
                logger.error("Error occured in Registering user");
                logger.error("Error:" + str(e.args))
        elif(type == "Register"):
            logger.info("Redirecting user to Register Page")
            return redirect('/webapp/register.html')
        else:
            logger.info("Startup Login Page");
            context = {'Status': "Please sign in", 'Error': "Null"}
            return render(request, 'webapp/login.html', context)
    
    def ajax_sendMail(request):
        logger = logging.getLogger('views.logger.sendEmail')
    
        sendEmail = request.POST['email']
        username = request.POST['username']
        error = request.POST['error']
    
        logger.info("Sending email to admins, sendEmail:" + sendEmail + ", username:" + username + ", error:" + error)
    
        if(sendEmail == "true"):
            mail_admins("User:" + username + " failed to " + error, "The time of error is at:" + datetime.datetime.now())
        return HttpResponse("Success in Sending Email")
    

login.html:

    
    
    <!DOCTYPE html>
    <html>
        <head>
            <!-- Load css -->
            {% load staticfiles %}
            <link rel="stylesheet" href="{% static 'WebApp/bootstrap-3.2.0-dist/css/bootstrap.min.css' %}">
            <link rel="stylesheet" type="text/css" href="{% static 'WebApp/login.css' %}"/>
    
            <title> WebStats Login </title>
        </head>
    
        <body>
            <!-- Load javascripts -->
            {% load staticfiles %}
            <script type="text/javascript" src="{% static 'WebApp/jquery-2.1.1.min.js' %}"></script>
            <script type="text/javascript" src="{% static 'WebApp/login.js' %}"></script>
            <script type="text/javascript" src="{% static 'WebApp/bootstrap-3.2.0-dist/js/bootstrap.min.js' %}"></script>
    
            <!-- Variables that we pass to javascript -->
            <script type="text/javascript">
                var errorMessage = "{{ Error }}";
                var username = "{{ username }}";
            </script>
    
            <!-- Inputs and Buttons -->
            <div class="container">
              <form class="form-signin" role="form" action="{% url 'WebApp:login'%}" method="post">
                {% csrf_token %}
                <h2 class="form-signin-heading">{{ Status }}</h2>
                <input type="username" id="username" name="username" class="form-control" placeholder="User Name" autofocus>
                <input type="password" id="password" name="password" class="form-control" placeholder="Password">
                <label class="checkbox">
                  <input type="checkbox" value="remember-me"> Remember me
                </label>
    
                <button class="btn btn-lg btn-primary btn-block" type="submit" value="Login" name="submit" id="login">Sign in</button>
                <button class="btn btn-lg btn-primary btn-block" type="submit" value="Register" name="submit" id="register">Register</button>
              </form>
            </div>
    
            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">Error during Sign In</h4>
                  </div>
                  <div class="modal-body">
                    <p> Possible Reasons: </p>
                    <ol>
                        <li> Wrong Username or Password </li>
                        <li> User does not exist </li>
                        <li> Login Server is down </li>
                        <li> Account is Disabled </li>
                        <li> Check your internet cable </li>
                        <li> Programming error done by the Tool Owner </li>
                        <br>
                        <form>
                        <button type="button" id="sendErrorEmail" value="send" class="btn btn-danger" data-dismiss="modal"> Report Problem </button>
                    </ol>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  </div>
                </div>
              </div>
            </div>
    
    
        </body>
    </html>
    

url.py:

    
    
    from django.conf.urls import patterns, include, url
    from django.contrib import admin
    from django.contrib.auth.views import logout
    from WebApp import views
    
    urlpatterns = patterns('',
        url(r'^index', views.index, name='index'),
        url(r'^login', views.loginUser, name='login'),
        url(r'^logout', views.logoutUser, name='logout'),
        url(r'^register', views.registerUser, name='register'),
        url(r'^sendMail', views.ajax_sendMail, name='sendMail'),
    )
    

login.js:

    
    
    var main = function()
    {
        if(errorMessage == "Invalid")
        {
            $('#myModal').modal("show");
        }
        else
        {
            $('#myModal').modal("hide");
        };
    
        $("#sendErrorEmail").click(function(event)
        {
            var csrftoken = getCookie('csrftoken');
            event.preventDefault();
            $.ajax(
            {
                type:"POST",
                url:"sendMail/",
                data:{
                    'email': "true",
                    'error': "login",
                    'username' : username,
                    'csrfmiddlewaretoken':csrftoken
                }
            });
            $('#myModal').modal("hide");
            return false;
        });
    };
    
    //To get the csrf token
    function getCookie(name) 
    {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') 
        {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) 
            {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) 
                {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }   
    $(document).ready(main);
    

I was able to send AJAX POST calls successfully, and in my views.py of the
function Ajax_sendEmail I was able to get my data. This is the logger details:

    
    
    2014-09-04 16:04:20,901 [INFO] views.logger.login: User:adfafasdfa requests:Login
    2014-09-04 16:04:20,901 [INFO] views.logger.login: UserName:adfafasdfa is trying to login
    2014-09-04 16:04:21,124 [INFO] views.logger.login: User:adfafasdfa is not valid
    2014-09-04 16:04:22,095 [INFO] views.logger.sendEmail: Sending email to admins, sendEmail:true, username:adfafasdfa, error:login
    

I was able to get sendEmail, username, and Error, however it still calls HTTP
500 MultiValueDictKeyError.

    
    
    MultiValueDictKeyError at /webapp/sendMail/
    "'email'"
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/webapp/sendMail/
    Django Version: 1.6.5
    Exception Type: MultiValueDictKeyError
    Exception Value:    
    "'email'"
    Exception Location: C:\Python27\lib\site-packages\django\utils\datastructures.py in __getitem__, line 301
    Python Executable:  C:\Python27\python.exe
    Python Version: 2.7.6
    

Does anyone know why this would happen? Very odd...

What is more strange is the chrome inspect element tool,

A screenshot of Chrome Inspect Element tool:

![enter image description here](http://i.stack.imgur.com/OLSl6.png)

It says that it is a POST, however, when you go inside:

![enter image description here](http://i.stack.imgur.com/pO6fq.png)

It shows a GET


Answer: I don't know!  
:)

Really, your code looks fine.

Is it possible that you are calling the 'sendMail/' URL twice? Once with
'POST' (that actually goes through), and once with `GET` from somewhere else?

I suggest littering that `ajax_sendMail()` function with calls to logger (just
temporarily, of course) to see exactly when the exception is being raised and
if the view might be called twice for some reason.

* * *

Also maybe add a `if request.method == 'POST':` condition in the first line of
`ajax_sendMail()` body. And it's always a good idea to do
`request.POST.get('some_key', default_if_not_existing)` when there is a
possibility that a key might not be there. This doesn't solve your problem,
but it might help debug it.

