0

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.

views.py

@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.

9
  • Did you add the url routing to urls.py? Commented Feb 7, 2013 at 9:33
  • 1
    If you just type the URL in the browser, it will return 404, so make sure you are POSTing something to it. Secondly, not sure why you aren't using django's ORM (did you know it can generate models from existing tables?). Finally, replace b==1 with int(b)==1 because 1 is a number and what you are getting from the URL mapper is the string '1'. Commented Feb 7, 2013 at 9:39
  • 1
    Btw, perhaps something went wrong in copy-pasting, but it looks like the indentation of your else statement is off by one space. That would actually raise a SyntaxError, so it's probably copy-paste, but I would advise to use 4 spaces for indentation; and don't put parentheses around expressions in if-statements and such. Have a read through PEP8. Commented Feb 7, 2013 at 10:45
  • 1
    Please don't use parens with if and elif. You don't need to write if (x == 1):. if x == 1: is enough. Commented Feb 7, 2013 at 11:14
  • 1
    Concerning tabs and spaces you should have a look at the Style Guide for Python Code. Commented Feb 7, 2013 at 11:16

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I also got as Burhan had suggested. Thanks to you too.
2

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.

Comments

2

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

4 Comments

I think print() with parenthesis is actually correct, since otherwise it would've returned a 500 and be a real syntax error; looks like this is simply Python 3.
oh yes, if python 3 than this parenthesis are correct. Thank you, I learned something new also :)
Even under Python 2 print with parentheses is syntactically correct; you're just adding an unnecessary grouping (for one argument) or changing your expected argument list into a single tuple.
Oh I see that. I will avoid writing parentheses around such comparison. I never thought it becomes a tuple by doing it.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.