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.

Community
  • 1
  • 1
John Doe
  • 2,752
  • 5
  • 40
  • 58
  • Did you add the url routing to ``urls.py``? – Fred Feb 07 '13 at 09:33
  • Yes the url is added to the urls.py. The value I am passing to the view function is getting printed in the shell – John Doe Feb 07 '13 at 09:38
  • 1
    If you just type the URL in the browser, it will return `404`, so make sure you are `POST`ing something to it. Secondly, not sure why you aren't using django's ORM (did you know it can [generate models from existing tables](https://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb)?). 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'`. – Burhan Khalid Feb 07 '13 at 09:39
  • Great Burhan Khalid Just Great. Problem fixed I just made the integer values into string. – John Doe Feb 07 '13 at 09:45
  • Sorry for not using django's ORM i have started reading about it, Pretty soon i will be using it. But thanks for the link. – John Doe Feb 07 '13 at 09:53
  • 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](http://www.python.org/dev/peps/pep-0008/). –  Feb 07 '13 at 10:45
  • Oh! Sorry it was pasting mistake. Okay I will use 4 spaces for indentation, right now I am using Tab, but i have heard senior guys not recommending it. Dont know the reason why? But i will stick to your advise. Thanks. – John Doe Feb 07 '13 at 10:51
  • 1
    Please don't use parens with `if` and `elif`. You don't need to write `if (x == 1):`. `if x == 1:` is enough. – Matthias Feb 07 '13 at 11:14
  • 1
    Concerning tabs and spaces you should have a look at the [Style Guide for Python Code](http://www.python.org/dev/peps/pep-0008/#tabs-or-spaces). – Matthias Feb 07 '13 at 11:16

4 Answers4

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

Community
  • 1
  • 1
iamkhush
  • 2,562
  • 3
  • 20
  • 34
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.

Tom Christie
  • 33,394
  • 7
  • 101
  • 86
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

Erika
  • 905
  • 1
  • 8
  • 15
  • 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. –  Feb 07 '13 at 09:45
  • oh yes, if python 3 than this parenthesis are correct. Thank you, I learned something new also :) – Erika Feb 07 '13 at 09:53
  • 1
    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. – GrandOpener Feb 07 '13 at 11:25
  • Oh I see that. I will avoid writing parentheses around such comparison. I never thought it becomes a tuple by doing it. – John Doe Feb 07 '13 at 11:40
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.

John Doe
  • 2,752
  • 5
  • 40
  • 58