John Doe
John Doe

Reputation: 2924

In Django not getting the requisite response

Here is my Server code which on execution is generating 500 error.

#views.py

import json

def registered_users_number(request):
    if not request.is_ajax():
       raise Http404

     usercount_today =  User.objects.raw("SELECT * FROM core_user WHERE DATE(reg_date) =   DATE(NOW())")
     print usercount_today
     response_data['result'] = usercount_today
     response_data['message']='You messed up'
     return HttpResponse(json.dumps(response_data), content_type="application/json")  

I guess Some syntax issues are there which I can figure out on account of my noviceness in Django and Python.

Also the print usercount_today is giving :- RawQuerySet: 'SELECT * FROM core_user WHERE DATE(reg_date) = DATE(NOW())'.

Thanks everyone

Upvotes: 0

Views: 109

Answers (3)

stephenmuss
stephenmuss

Reputation: 2445

Without seeing some more of your code it is not entirely possible to tell but it seems like you are using raw SQL when you could more easily harness the power of the Django ORM.

To me it looks like you could do it much more cleanly along the following lines:

import datetime

def registered_users_number(request):
    usercount_today = User.objects.filter(reg_date__gte=datetime.date.today()).count()
    response_data = simplejson.dumps({"count": usercount_today})
    return HttpResponse(response_data, content_type="application/json")

The main problem is we can't see your User model and it's hard to tell whether reg_date is already a datetime field or not. If not, I'd suggest using something like models.DateTimeField(auto_now_add=True) for your reg_date field and then you can easily use the ORM like above.

Upvotes: 1

John Doe
John Doe

Reputation: 2924

Thanks everyone.

I got it. It was nothing but lack of familiarity with Django.

Here is the changes that i did.

views.py

def registered_users_number(request):   
    from django.db import connection
    cursor = connection.cursor()
    if not request.is_ajax():
         raise Http404

    cursor.execute("SELECT COUNT(*) FROM core_user WHERE DATE(reg_date) = DATE(NOW())")
    usercount_today = cursor.fetchone()  
    print(usercount_today)
    response_data = simplejson.dumps({"count": usercount_today})
    return HttpResponse(response_data, content_type="application/json") 

Upvotes: 0

Павел Тявин
Павел Тявин

Reputation: 2669

I think you have three issues in code:

  1. Error on declaring response_data. Before line response_data['result'] = usercount_today you should define response_data = {}

  2. Why do you need to print usercount_today? It's a server code and python doesn't print this line into HttpResponse.

  3. Do you really need raw sql query on user objects retrieving? Why don't you use django ORM in this line? It's not only about code style, but also about Django: since 1.4 it saves time objects in a specific way in db, and you should use date-aware datetime objects instead DATE(NOW()).

Upvotes: 1

Related Questions