V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ciba1990
V2EX  ›  Python

Django 中 request.GET 获取 form 中的值

  •  
  •   ciba1990 · 2015-07-12 01:19:37 +08:00 · 4641 次点击
    这是一个创建于 3213 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前天用V友方法request.GET确实能够获取form的值,但是放到URL里面就会报错“name 'request' is not defined”,把代码中str(link[0])换成request.GET.get('q','')却又能传输到变量,是赋值问题还是其他什么问题。
    报错内容:
    NameError at /search/
    request' is not defined
    /home/mysite/urls.py in <module>
    url(r'^search/', include('search.urls')), ...
    ▶ Local vars

    views.py代码
    from bs4 import BeautifulSoup
    import urllib2
    from django.template import Context,loader
    from django.http import HttpResponse
    keywords = str(request.GET.get('q',''))
    url='https://www.baidu.com/s?wd=%s' % keywords
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')
    def index(request):
    template = loader.get_template('index.html')
    context = Context({'keyword':str(link[0])})
    return HttpResponse(template.render(context))

    11 条回复    2015-07-12 10:24:10 +08:00
    ciba1990
        1
    ciba1990  
    OP
       2015-07-12 01:27:53 +08:00
    新手提问,多多包含。
    Septembers
        2
    Septembers  
       2015-07-12 01:33:24 +08:00
    因为模板默认不会注入任何信息 也就是说你需要手动注入
    ciba1990
        3
    ciba1990  
    OP
       2015-07-12 01:36:05 +08:00
    @Septembers 我只是想把request.GET的值传到keywords这个变量,如果不能传,那为什么可以传给keyword这个变量。
    nealv2ex
        4
    nealv2ex  
       2015-07-12 01:57:02 +08:00 via iPhone
    “name 'request' is not defined
    这个报错信息麻烦lz解释一下什么意思

    keywords = str(request.GET.get('q',''))

    这里的request是什么,哪里来的,那个生命周期里存在这个request

    错误提示明明说的很清楚,难道还不会放到翻译软件翻译一下?
    ciba1990
        5
    ciba1990  
    OP
       2015-07-12 01:59:15 +08:00
    @nealv2ex
    request.GET放到Context()就可以,放到外面就不行,是不是赋值方式不对。
    nealv2ex
        6
    nealv2ex  
       2015-07-12 02:03:32 +08:00 via iPhone   ❤️ 1
    def index(request):

    你猜这句什么意思

    lz写代码完全靠猜啊,没有语言基础,不看框架教程
    ciba1990
        7
    ciba1990  
    OP
       2015-07-12 02:06:22 +08:00
    @nealv2ex
    我放到里面了 还是报错,
    keywords没定义
    url='https://www.baidu.com/s?wd=%s' % keywords
    ciba1990
        8
    ciba1990  
    OP
       2015-07-12 02:08:51 +08:00
    @nealv2ex 好了,明白了,搞定了。
    nealv2ex
        9
    nealv2ex  
       2015-07-12 02:12:20 +08:00 via iPhone
    keywords = str(request.GET.get('q',''))
    url='https://www.baidu.com/s?wd=%s' % keywords
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')


    全部放进去,原因嘛,反正你也不在意。
    已经被骗不少铜币了。
    ciba1990
        10
    ciba1990  
    OP
       2015-07-12 02:18:02 +08:00
    @nealv2ex
    这种方式可以的,但是填中文会提示q编码不对,
    'ascii' codec can't encode characters in position 30-32: ordinal not in range(128)
    MrEggNoodle
        11
    MrEggNoodle  
       2015-07-12 10:24:10 +08:00   ❤️ 1
    Django新人吗?那我答非所问的分享一些我的入门资料给你~
    http://www.ziqiangxuetang.com/django/django-tutorial.html
    这是一个非常好关于Django的简明教程,国内的,希望对你有帮助。

    http://django-china.cn
    这是一个国内相对活跃的Django论坛。他的Q群很活跃。(155719011)

    http://www.cnblogs.com/fnng/p/3737964.html
    这是一个叫虫师的Blog,有几篇不错的Django入门教程,完整的小项目。

    http://study.163.com/course/introduction/320022.htm#/courseDetail
    这是网易云课堂的一个Django课程,免费的~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   923 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:45 · PVG 03:45 · LAX 12:45 · JFK 15:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.