Reputation: 2924
I am new to python and Django, if its just some silliness please condone. This is my code in views.py. Its showing Page not found 404 error. Is there any syntax error.
Here on the basis of different values of b I am querying different data which I am rendering_to_response.
@csrf_exempt
def active_user_table(request, b):
if request.method != "GET":
raise Http404
print(b)//Just cheking the correct value coming to the function.This is getting printed in the shell.
if (b==4):
print("Hello World!")
cursor = connection.cursor()
cursor.execute("SELECT core_user.id, name,mobile_number,COUNT(*) as count, created FROM core_user,core_useractivity WHERE core_user.id = core_useractivity.user_id GROUP BY user_id ORDER BY count DESC")
response_data = dictfetchall(cursor)
return render_to_response("siteadmin/active_user_table.tmpl",{'response_data':response_data})
elif (b==3):
print("Hello World!")
cursor = connection.cursor()
cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND MONTH(CAST(created as date)) = MONTH(NOW()) AND YEAR(cast(created as date)) = YEAR(NOW()) GROUP BY user_id ORDER BY count DESC")
response_data = dictfetchall(cursor)
return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b==2):
cursor = connection.cursor()
cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATEDIFF(NOW(), created) <= 7 GROUP BY user_id ORDER BY count DESC")
response_data = dictfetchall(cursor)
return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b==1):
cursor = connection.cursor()
cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATE(created) = DATE(NOW())")
response_data = dictfetchall(cursor)
return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
else:
raise Http404
If I am removing else: part Its showing no HttpResponse sent. I dont know what could be wrong.
Upvotes: 0
Views: 470
Reputation: 2924
As Burhan Khalid first suggested the value we get from the url mapper is a string. So only change required was to make all integers in the comparisons strings.
Upvotes: 0
Reputation: 925
It seems like your b is none of these values, which could be either the fact that is not passed to the view (this you can check in your urls.py) or even that you should put it to int(b) if maybe it does not understand what it is.
try "print b" and "print type(b)" # without your original parenthesis and see what you had returned
Upvotes: 2
Reputation: 33901
Note that 'b' in this case will be a string, as it's a portion of the URL, not an integer.
If you change your tests to eg. b=='1'
, and make sure you raise Http404 if they all fail, then things should work as you're expecting.
Upvotes: 2
Reputation: 2592
the arg b will be a string, and you are assuming it is a number. Convert numbers to string and then compare with b. Such as b == '1'.
You clearly lack debugging skills. As you said, you are new to this, please go through pdb module. This module puts a breakpoint in your code.
There are many helpful posts , such as - Getting started with the Python Debugger pdb
Upvotes: 2