Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)

139 views
Skip to first unread message

Akhi

unread,
Jun 12, 2017, 3:07:02 PM6/12/17
to Django users

I'm running Html file from S3 bucket in that HTML file there is AJAX call when I run/open the HTML file I am getting below error

Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)

My code from s3 bucket JS file

<script type="text/javascript">

$(document).ready(function () {

    $.get("https://52.221.186.121/email_view/6/", function (data) {
        console.log(JSON.stringify(data));
        email_otp = JSON.stringify(data);
        $("#btnSubmit").click(function () {
            var input_value = $('#entered_otp').val();
            alert(input_value);
            alert(email_otp);
            if (email_otp == input_value) {
                alert('matches');
                $('.modal').removeClass('show');

            }
            else {
                alert('not matching');

                window.location = "https://awslocation.com/mobile/index.html";

            }
        });
    });


});

Corresponding views.py code

def email_view(request, id):
    course = get_object_or_404(CourseWare, pk=id)
    user = UserProfile.objects.get(user__id=request.user.id)
    myorder = MyOrder.objects.get_or_create(buyer=user, course=course)
    if myorder:
        sms_otp = randint(10000, 100000)
        user = UserProfile.objects.get(user__id=request.user.id)
        email = user.user.email
    body = '''Dear User, \n Please Enter the OTP to view the Course.
                                          \n \n OTP : %s \n \n Thanks,''' % (
        sms_otp)
        email = EmailMessage('Course Ware OTP', body, to=[email])
        email.send()
    if request.is_ajax():
        return HttpResponse(json.dumps(sms_otp), content_type="application/json")

taceback

27.6.180.134 - - [12/Jun/2017:17:24:40 +0000] "GET /email_view/6/ HTTP/1.1" 500 38 "url_from_html_page_in_s3_bucket" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
27.6.180.134 - - [12/Jun/2017:17:53:59 +0000] "GET /email_view/6/ HTTP/1.1" 500 38 "url_from_html_page_in_s3_bucket" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"

Melvyn Sopacua

unread,
Jun 12, 2017, 7:36:18 PM6/12/17
to django...@googlegroups.com

On Monday 12 June 2017 11:18:52 Akhi wrote:

 

> def email_view(request, id):

> course = get_object_or_404(CourseWare, pk=id)

> user = UserProfile.objects.get(user__id=request.user.id)

> myorder = MyOrder.objects.get_or_create(buyer=user, course=course)

 

Return value for get_or_create() is tuple(obj: models.Model, created: bool).

So your if check is useless as it is always true. But your method signature is incorrect as well. You're not supplying defaults.

 

On top of that, you fetch the user profile twice and both cases can blow up if no profile exists.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages