推荐学习书目
› 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 · Nov 13, 2010 · 5965 views
    This topic created in 5799 days ago, the information mentioned may be changed or developed.
    大家好,刚刚注册的新人,请各位多关照~

    本人是大学生,喜欢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 replies  •  1970-01-01 08:00:00 +08:00
    jjgod
        1
    jjgod  
       Nov 13, 2010
    if "img_head" in request.FILES:
    blahblahblah
    reorx
        2
    reorx  
    OP
       Nov 13, 2010
    @jjgod 多谢啦,这样做是正确的!问题解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   856 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:35 · PVG 05:35 · LAX 14:35 · JFK 17:35
    ♥ Do have faith in what you're doing.