免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5394 | 回复: 10
打印 上一主题 下一主题

django的一个弱智,但是困扰我的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-10 15:24 |只看该作者 |倒序浏览
模板
<html>
<head>
  <title>用户账号为</title>
</head>
<body>
{% if aaa %}      
<form method="post" action="/an/">
        请输入要查询的角色名
        <input type="text" name="aaa" value="">
        <input type="submit" value="玩家账号查询">
              </form>
  {{ data1 }}
</body>
</html>

view.py

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
import MySQLdb
def userinfo(request):
    zjj = request.POST.get('aaa',None)
    if zjj:
        db = MySQLdb.connect(user='root', db='sunyou', passwd='sunyou', charset='utf8',host='192.168.1.234')
        cursor = db.cursor()
        cursor.execute('SELECT name FROM account where id=(select acct from characters where name="wad")')
        data1 = cursor.fetchall()
        db.close()
        return render_to_response('an.html',{'bbb':data1})


报错
ValueError at /an/
The view mysite.zhanghao.userinfo didn't return an HttpResponse object.
Request Method: GET
Request URL: http://127.0.0.1:8000/an/
Exception Type: ValueError
Exception Value: The view mysite.zhanghao.userinfo didn't return an HttpResponse object.
Exception Location: E:\Python25\Lib\site-packages\django\core\handlers\base.py in get_response, line 94
求救啊各位高人
ps:另外问下各位
django是如何区分一个页面里的不同的request的?
谢谢谢谢!

论坛徽章:
0
2 [报告]
发表于 2009-07-10 15:42 |只看该作者
首先模板里缺少{% endif %}

论坛徽章:
0
3 [报告]
发表于 2009-07-10 15:44 |只看该作者
if zjj:
     是否到这个分支了

论坛徽章:
0
4 [报告]
发表于 2009-07-10 15:45 |只看该作者

  1. def userinfo(request):
  2.     zjj = request.POST.get('aaa',None)
  3.     if zjj:
  4.         db = MySQLdb.connect(user='root', db='sunyou', passwd='sunyou', charset='utf8',host='192.168.1.234')
  5.         cursor = db.cursor()
  6.         cursor.execute('SELECT name FROM account where id=(select acct from characters where name="wad")')
  7.         data1 = cursor.fetchall()
  8.         db.close()
  9.         return render_to_response('an.html',{'bbb':data1})
复制代码

这个写法也有问题,只有zjj为真才执行下面的程序,如果假就是return None了。

另外你的写法太乱,不要起什么aaa bbb的,自己看的不晕么

论坛徽章:
0
5 [报告]
发表于 2009-07-10 15:46 |只看该作者
{% if not username %}
<form method="post" action="/login/">
    用户名:<input type="text" name="username" value=""><br/>
    <input type="submit" value="登录">
</form>
{% else %}
你已经登录了!{{ username }}<br/>
<form method="post" action="/logout/">
    <input type="submit" value="注销">
</form>
{% endif %}
加了endif
报一样的错
这个问题我改了很久
目前认为不是模板的问题
是view的问题
从报错信息上看
我认为是render_to_response的问题
但是具体怎么解决还不清楚。。。。。。

论坛徽章:
0
6 [报告]
发表于 2009-07-10 15:50 |只看该作者
原帖由 bohemia 于 2009-7-10 15:44 发表
if zjj:
     是否到这个分支了


print zjj
是None吧?

论坛徽章:
0
7 [报告]
发表于 2009-07-10 16:23 |只看该作者
The view mysite.zhanghao.userinfo didn't return an HttpResponse object.
Request Method: GET

zjj = request.POST.get('aaa',None)

看这个就知道了zjj肯定是None了。我已经说了if zjj那行不对了

论坛徽章:
0
8 [报告]
发表于 2009-07-10 16:58 |只看该作者
原帖由 smallfish_xy 于 2009-7-10 16:23 发表
The view mysite.zhanghao.userinfo didn't return an HttpResponse object.
Request Method: GET

zjj = request.POST.get('aaa',None)

看这个就知道了zjj肯定是None了。我已经说了if zjj那行不对了


我也看出来时这句不对劲了
不过请教下
我想实现一旦在aaa这个空里填入东西后,再按了“玩家账号查询”,就显示出我从数据库里查到的信息
怎么改才对呢?
急!
谢谢!
谢谢!

论坛徽章:
0
9 [报告]
发表于 2009-07-10 17:20 |只看该作者
an.html文件为

用户账号为     
<form method="get" action="/an/">
        请输入要查询的角色名    {{ data1 }}
        <input type="text" name="aaa" value="">
        <input type="submit" value="玩家账号查询">
              </form>

zhanghao.py文件为

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
import MySQLdb
def userinfo(request):
#zjj = request.GET.get('aaa',None)
   # if  not zjj:
        db = MySQLdb.connect(user='root', db='sunyou', passwd='sunyou', charset='utf8', host='192.168.1.234')
        cursor = db.cursor()
        cursor.execute('SELECT name FROM account where id=(select acct from characters where name="wad")')
        data1 = cursor.fetchall()
        db.close()
        return render_to_response('an.html',{ 'aaa' : data1 })
    #else:
     #   return render_to_response('an.html'}

这么改后顺利显示出界面
但是{{aaa}}这个变量无法显示
也不知道怎么把这个变量传到模板里
谁给指教下?

论坛徽章:
0
10 [报告]
发表于 2009-07-10 17:28 |只看该作者
原帖由 zjjzjj1002 于 2009-7-10 17:20 发表
an.html文件为

用户账号为     

        请输入要查询的角色名    {{ data1 }}
        
        
              

zhanghao.py文件为

from django.http import HttpResponseRedirect
from djan ...

找找django book里面模板那一章节仔细看看把
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP