我找过那些经历过我目前遇到的同样问题的帖子。但我还没有找到解决方案。我的问题是:
我有一个标签列表。由{% for tag in all_tags %}
生成。每个标签都有一个tagstatus形式。当用户从下拉列表中选择一个选项时,表单会提交并且应该保存TagStatus对象(标签对TagStatus是foreignKey'd)。但是,返回的是:
Exception Type: TypeError
Exception Value: 'QueryDict' object is not callable
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:
@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 (如果相关):
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)
从我收集的内容来看,这与状态不是数字有关。但是当我将它转换为int时。我犯了同样的错误。请帮我。为什么会这样?什么是修复?我不太清楚如何解决这个问题。提前谢谢你的帮助。
答案 0 :(得分:27)
我相信您遇到的错误在以下几行:
status = request.GET('status')
tag = request.GET('tag')
request.GET是一个QueryDict,并在尝试“调用”一个不可调用的对象后添加()。您正在寻找的语法似乎是字典查找语法:
status = request.GET['status']
tag = request.GET['tag']
答案 1 :(得分:3)
或只是致电GETs获取你的价值
status = request.GET.get('status')
tag = request.GET.get('tag')
答案 2 :(得分:1)
db.product_cats.find().toArray().sort( (a, b) => {
let ixa = a.tags.findIndex(e => e.tag == "category");
let ixb = b.tags.findIndex(e => e.tag == "category");
if ( ixa == -1 ) { a.tags = [ ]; a.tags[ ixa] = { value: "" } };
if ( ixb == -1 ) { b.tags = [ ]; b.tags[ ixa] = { value: "" } };
return a.tags[ixa].value > b.tags[ixb].value;
});
答案 3 :(得分:0)
status = request.GET.get['status']
tag = request.GET.get['tag']