在 Django 视图中,如何更改 class 成员的值?
In Django view, how can I change the value of the class member?
如果候选人获得选票,我想增加他的 收入 价值。所以我把这部分写在了result_view里面,但是不行。
这是我的看法。
from django.shortcuts import render_to_response
from election.models import *
# /candidate/ view
def candidate_view(request):
if request.method == 'GET':
c = Candidate.objects.all()
return render_to_response('candidate.html', {'candidates': c})
# /candidate/result/ view
def result_view(request):
if request.method == 'POST':
c = Candidate.objects.all()
ec = Candidate.objects.filter(num=request.POST.get('choice'))
ec[0].earn += 1
ec[0].name = 'semi kwon'
ec[0].save()
#return render_to_response('result.html', {'candidates': ec})
return render_to_response('result.html', {'candidates': c})
这是我的模型。
from django.db import models
class Candidate(models.Model):
num = models.IntegerField()
name = models.TextField()
major = models.TextField()
grade = models.IntegerField()
earn = models.IntegerField(default = 0, blank=True, editable=True)
def __unicode__(self):
return "%d : %s"%(self.num, self.name,)
我该如何解决这个问题?
您更新候选人后是否检查过数据库?
由于您在更新候选人 (ec) 之前获取候选人 (c),您可能已经更新了候选人但将旧集合传递给了模板。
最好使用表格,但为了保持逻辑,试试这个:
def result_view(request):
# csrf update etc
# your request.method GET here
# ...
if request.method == 'POST':
# dictionary with vars that will be passed to the view
params = {}
# check if there is a candidate with this num
# before you do anything with it
# you could do a try/except instead
if Candidate.objects.filter(num=request.POST.get('choice')).count():
# Getting candidate
candidate = Candidate.objects.get(num=request.POST.get('choice'))
# Updating candidate
candidate.earn += 1
# I don't know what this is, but let's keep it
candidate.name = 'semi known'
# Saving candidate
candidate.save()
else:
# returning error, candidate not found
params['error'] = "No candidate with this num"
# updating candidates list
params['candidates'] = Candidate.objects.all()
# rendering view
return render_to_response('result.html', params)
如果候选人获得选票,我想增加他的 收入 价值。所以我把这部分写在了result_view里面,但是不行。
这是我的看法。
from django.shortcuts import render_to_response
from election.models import *
# /candidate/ view
def candidate_view(request):
if request.method == 'GET':
c = Candidate.objects.all()
return render_to_response('candidate.html', {'candidates': c})
# /candidate/result/ view
def result_view(request):
if request.method == 'POST':
c = Candidate.objects.all()
ec = Candidate.objects.filter(num=request.POST.get('choice'))
ec[0].earn += 1
ec[0].name = 'semi kwon'
ec[0].save()
#return render_to_response('result.html', {'candidates': ec})
return render_to_response('result.html', {'candidates': c})
这是我的模型。
from django.db import models
class Candidate(models.Model):
num = models.IntegerField()
name = models.TextField()
major = models.TextField()
grade = models.IntegerField()
earn = models.IntegerField(default = 0, blank=True, editable=True)
def __unicode__(self):
return "%d : %s"%(self.num, self.name,)
我该如何解决这个问题?
您更新候选人后是否检查过数据库?
由于您在更新候选人 (ec) 之前获取候选人 (c),您可能已经更新了候选人但将旧集合传递给了模板。
最好使用表格,但为了保持逻辑,试试这个:
def result_view(request):
# csrf update etc
# your request.method GET here
# ...
if request.method == 'POST':
# dictionary with vars that will be passed to the view
params = {}
# check if there is a candidate with this num
# before you do anything with it
# you could do a try/except instead
if Candidate.objects.filter(num=request.POST.get('choice')).count():
# Getting candidate
candidate = Candidate.objects.get(num=request.POST.get('choice'))
# Updating candidate
candidate.earn += 1
# I don't know what this is, but let's keep it
candidate.name = 'semi known'
# Saving candidate
candidate.save()
else:
# returning error, candidate not found
params['error'] = "No candidate with this num"
# updating candidates list
params['candidates'] = Candidate.objects.all()
# rendering view
return render_to_response('result.html', params)