我是 python 和 Django 的新手,如果只是有些愚蠢,请宽恕。这是我在views.py 中的代码。它显示页面未找到 404 错误。有没有语法错误。
在这里,根据 b 的不同值,我正在查询我正在渲染的不同数据。
视图.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
如果我要删除 else: 部分它显示没有发送 HttpResponse。我不知道有什么问题。