为什么我在django中得到MultiValueDictKeyError?

时间:2015-05-06 21:34:46

标签: django django-views

在我添加图片字段headimage之前,我有一个文章应用程序曾经工作正常。

以下是模型:

class Article(models.Model):
    nid = models.IntegerField(default=0)
    headimage = ImageWithThumbsField(upload_to='images', blank=True, sizes=((200,200),(400,400))
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100, blank=True)
    translator = models.CharField(max_length=100, blank=True)
    body = models.TextField()
    teaser = models.TextField('teaser', blank=True)
    created=models.DateTimeField(default=datetime.datetime.now)
    pub_date=models.DateTimeField(default=datetime.datetime.now)
    categories = models.ManyToManyField(Category, blank=True)
    slug = models.CharField(max_length=100, blank=True)
    tags = TaggableManager()

观点:

#@staff_member_required
def add_article(request):
    if request.method == 'POST':
        dform = ArticleForm(request.POST, request.FILES)
        if dform.is_valid():
            form = Article(headimage = request.FILES['headimage'])           
            form.save(commit=False)

            form.user = request.user
            form.save()
            form.save_m2m()

            messages.info(request, "article was added") 
            #args = {}
            post = Article.objects.latest('id')
            post_slug = post.slug
            return redirect('article.views.post_withslug', post_slug=post_slug)
        else:
            print "form not valid"

    if request.method == 'GET':
        form = ArticleForm()
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'article/add_article.html', args)    

模板:

<form   action="{% url "article.views.add_article" %}" method="post">{% csrf_token %}
 <div class="form-group">
    <label>Head Image</label>
    <input type="file" id="id_headimage" name="headimage" >
  </div>

 <div class="form-group">
    <label>Title</label>
    <input id="id_title" maxlength="100" name="title" type="text">
  </div>  

     <div class="form-group">
    <label>Author</label>
     <input id="id_author" maxlength="100" name="author" type="text">
  </div>

   <div class="form-group">
    <label>Translator</label>
     <input id="id_translator" maxlength="100" name="translator" type="text">
  </div>

      <div class="form-group">
    <label>Body</label>
     {% include "texteditor.html" %}
      <textarea id="id_body" name="body" rows="20" cols="180"></textarea>
  </div>

        <div class="form-group">
    <label>tags</label>
      <input id="id_tags" maxlength="100" name="tags" type="text">
  </div>


<input type="submit" name="submit"  value="Update" />
</div>

</form>

值得注意的是 1.图像字段使用我在其他地方成功使用的this出色的图像大小调整片段。 2.即使没有在表单中发布图像,我也会收到错误。

我在视图上尝试了不同的技巧,但在此持续了好几个小时。非常感谢你的帮助。

更新:这是引用:

环境:

Request Method: POST
Request URL: http://127.0.0.1:8000/add/article

Django Version: 1.8
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'contact',
 'captcha',
 'article',
 'debug_toolbar',
 'userpics',
 'taggit')
Installed Middleware:
(u'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/home/randi/.env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/randi/proj1/article/views.py" in add_article
  90.             form = Dastan(headimage = request.FILES['headimage'])           
File "/home/randi/.env/local/lib/python2.7/site-packages/django/utils/datastructures.py" in __getitem__
  322.             raise MultiValueDictKeyError(repr(key))

Exception Type: MultiValueDictKeyError at /add/article
Exception Value: "'headimage'"

2 个答案:

答案 0 :(得分:2)

您错过了HTML表单标记中的enctype="multipart/form-data"。否则,浏览器不会发送任何文件数据。

尽管如此,您不应该直接在视图中引用request.POSTrequest.FILES字母:那是表单的用途:

    dform = ArticleForm(request.POST, request.FILES)
    if dform.is_valid():
        article = dform.save(commit=False)

        article.user = request.user
        article.save()
        dform.save_m2m()

答案 1 :(得分:1)

我不确定这是否可以解决问题,但是我可以看到您正在正确保存表单。无论发布的数据如何,您都会在提交后初始化新对象。我发现你的模型中有必须使用的字段

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}