Python Django TypeError 'QueryDict' 对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15799328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:02:55  来源:igfitidea点击:

Django TypeError 'QueryDict' object is not callable

pythondjangodjango-formsdjango-views

提问by Modelesq

I've looked for posts that have endured the same problem that I'm currently facing. But I haven't found a solution. What my problem is:

我一直在寻找与我目前面临的相同问题的帖子。但我还没有找到解决办法。我的问题是:

I have a list of tags. Generated by {% for tag in all_tags %}. Each tag has a tagstatus form. When a user selects an option from a drop down, the form submits and is supposed to save the TagStatus object (Tags are foreignKey'd to TagStatus). However, what returns is this:

我有一个标签列表。由{% for tag in all_tags %}. 每个标签都有一个 tagstatus 形式。当用户从下拉列表中选择一个选项时,表单提交并应该保存 TagStatus 对象(Tags are foreignKey'd to TagStatus)。然而,返回的是这样的:

Exception Type: TypeError
Exception Value: 'QueryDict' object is not callable

html:

html:

<form class="nice" id="status-form" method="POST" action="">
     {% csrf_token %}
     <input type="hidden" name="status_check" />
     <input type='hidden' name="tag" value="{{ tag }}" />
     <select name="select" id="positionSelect" class="input-text category" onchange="this.form.submit()">
          <option name="all" value="0">Your Status</option>
          <option name="investing" value="1">Status 1</option>
          <option name="selling" value="2">Status 2</option>
          <option name="interested" value="3">Status 3</option>
     </select>
</form>

views.py:

视图.py:

@login_required
def tags(request):
    all_tags = Tag.objects.all()
    context = base_context(request)
    if request.method == 'POST':
        if 'status_check' in request.POST:
            status = request.GET('status')
            tag = request.GET('tag')
            user = request.user
            tag_status, created = TagStatus.objects.get_or_create(status=len(status), tag=tag, user=user).save()

            response = simplejson.dumps({"status": "Successfully changed status"})
        else:
            response = simplejson.dumps({"status": "Error"})
            return HttpResponse (response, mimetype='application/json')
    context['all_tags'] = all_tags
    return render_to_response('tags/tag.html', context, context_instance=RequestContext(request))

models.py(if its relevant):

models.py(如果相关):

class TagStatus(models.Model):
    user = models.ForeignKey(User, null=True, unique=True)
    status = models.CharField(max_length=2, choices=tag_statuses)
    tag = models.ForeignKey(Tag, null=True, blank=True)

    def __unicode__(self):
        return self.status

    def save(self, *args, **kwargs):
        super(TagStatus, self).save(*args, **kwargs)

From what I gather it has something to do with the status not being a number. But when I convert it to a int. I get the same error. Please help me. Why is this happening? And whats the fix? I'm not quite sure how to solve this problem. Thank you for your help in advance.

据我所知,这与状态不是数字有关。但是当我将它转换为 int 时。我犯了同样的错误。请帮我。为什么会这样?什么是修复?我不太确定如何解决这个问题。提前谢谢你的帮助。

采纳答案by Jeffrey Froman

I believe the error you're encountering is in these lines:

我相信您遇到的错误在这些行中:

status = request.GET('status')
tag = request.GET('tag')

request.GET is a QueryDict, and adding () after it attempts to "call" a non-callable object. It appears the syntax you're looking for is dictionary lookup syntax instead:

request.GET 是一个 QueryDict,并在它尝试“调用”一个不可调用的对象后添加 ()。您正在寻找的语法似乎是字典查找语法:

status = request.GET['status']
tag = request.GET['tag']

回答by f-re

or just call GETs get to get your values

或者只是调用 GETs get 来获取你的值

status = request.GET.get('status')
tag = request.GET.get('tag')

回答by Mukund Biradar

def add(request):
    var1 = int(request.GET.get("num1"))
    var2 = int(request.GET.get("num2"))

    res= var1+var2
    return render(request,'calc/result.html',{'Result':res})