无法使用django更新模型的数据

时间:2016-05-30 12:20:28

标签: python django

我希望当我使用表单进行查询时,模型SearchAllStatics的{​​{1}}会添加一个。但事实是,它仍然是一个。

这是models.py

view_count

views.py

class SearchAllStatistic(models.Model):

    content = models.CharField(max_length=255)
    view_count = models.IntegerField(default=0)

    def __unicode__(self):
        return self.content

    class Meta:
        ordering = ['-view_count']

这是模板

class SearchWord(generic.View):

    def get(self, request):
        word = request.GET['field3']
        object = SearchAllStatistic.objects.filter(content=word)

        if object:
            w = SearchAllStatistic.objects.get(content=word)
            w.view_count =+1
            print(w.view_count)
            w.save()
        else:
            w = SearchAllStatistic(content=word, view_count=10)
            w.save()
        try:
            form = Search_Shiwen_Form(request.GET)
            if form.is_valid():
                shiwen = Shiwen.objects.get(field3=form.cleaned_data['field3'])
                return render(request, 'search/shiwen.html', {'result': shiwen})
            else:
                return render(request, 'search/errors.html', {'errors': 'xx'})
        except:
            form = Search_word_Form(request.GET)
            if form.is_valid():
                word = Newgw.objects.filter(field2=form.cleaned_data['field3'])
                return render(request, 'search/word.html', {'result': word})
            else:
                return render(request, 'search/errors.html')

我想知道为什么view_count没有更新,只是为了<form id="form" method="GET" action="word/"> <label for="id_field3"></label><input id="id_field3" maxlength="255" name="field3" type="text" /> <button type="submit">xxx</button> </form>

1 个答案:

答案 0 :(得分:1)

你有错误

    w.view_count =+1

1值分配给view_count变量而不是

    w.view_count += 1

正在使用+=运算符你想要做什么