TypeError:&#39;在<string>&#39;需要字符串作为左操作数,而不是QueryDict

时间:2016-10-12 13:14:59

标签: python python-3.x

在更新已编辑的博客时,我收到此错误:在edit_article函数中 这是我的功能,

def edit_article(request, id):
    session_start(request)
    if Article.exists(id):
        article = Article.getByName(id)
    else:
        article = Article(id)
    if request.method == 'POST' and request.POST in "content":
        if has_article_access(request, article):
            article.body = request.POST['content']
            article.save()
            if 'counter_edit' in request.session:
                request.session['counter_edit'] += 1
            else:
                request.session['counter_edit'] = 1
            delete_article_locked(request, article)
            return HttpResponseRedirect('/myblog/?edited')
        else:
            return HttpResponseRedirect('/myblog/?locked')
    else:
        if has_article_access(request, article):
            start_article_locked(request, article)
        else:
            return HttpResponseRedirect('/myblog/?locked')
        return render_to_response("edit.html",
                                  {
                                   'name': article.title,
                                   'content': article.body,
                                   'id':article.id
                                  },
                                  context_instance=RequestContext(request)
        )

错误显示在第7行。

1 个答案:

答案 0 :(得分:1)

如果您想检查request.POST是否有密钥"content"

if request.method == 'POST' and "content" in request.POST: