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
reorx
V2EX  ›  Python

问一个关于django的简单问题

  •  
  •   reorx ·
    reorx · 2010-11-13 02:32:00 +08:00 · 4888 次点击
    这是一个创建于 4885 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大家好,刚刚注册的新人,请各位多关照~

    本人是大学生,喜欢python,业余时间学习django做web开发。

    今晚遇到这样一个问题
    member/models.py中部分代码
    class Member(models.Model):
    img_head = models.ImageFiled(u'head image', upload_to = 'img_head/')

    我在页面中做了一个提交表单,<input type="file" name="img_head" />,views中写了一个if判断,希望在表单为空的时候不要写入数据库
    i = request.FILES["img_head"]
    if i != '':
    member.img_head = i
    member.save()

    结果是如果有图片上传,一切正常,如果表单留空,会出现如下报错:
    MultiValueDictKeyError at /member/register_register/
    "Key 'img_head' not found in <MultiValueDict: {}>"

    我想问题可能出现在request.FILES["img_head"]的数据类型上,应该不是字符串,究竟是啥现在还没搞清,还请大家帮忙看看,if那句肯定是有问题的
    2 条回复    1970-01-01 08:00:00 +08:00
    jjgod
        1
    jjgod  
       2010-11-13 04:06:27 +08:00
    if "img_head" in request.FILES:
    blahblahblah
    reorx
        2
    reorx  
    OP
       2010-11-13 10:58:18 +08:00
    @jjgod 多谢啦,这样做是正确的!问题解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2794 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:21 · PVG 20:21 · LAX 05:21 · JFK 08:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.