我有一个简单的视图函数,旨在允许用户从 html 表(记录)中列出的项目中进行选择。单击记录应将用户转移到可以编辑该特定记录的模板。代码如下:
def edit_record(request):
if request.method == 'POST':
a=ProjectRecord.objects.get()
form = RecordForm(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
a=ProjectRecord.objects.get()
form = RecordForm(instance=a)
return render_to_response('productionModulewire.html', {'form': form})
问题是该函数仅在数据库中只有 1 条记录时才能完美运行。一旦我添加另一个,我就会收到一个多次返回的项目错误。我怀疑它与“objects.get()”有关,但我不知道如何正确构建视图?
网址很简单(也许太多了):
(r'^edit/', edit_record),
模型如下所示:
class ProjectRecord(models.Model):
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
account = models.CharField(max_length=50, choices=ACCOUNT_CHOICES)
project_type = models.CharField(max_length=50, choices=TYPE_CHOICES)
market = models.CharField(max_length=50, choices=MARKET_CHOICES)
agencyID = models.CharField(max_length=30, unique=True, blank=True, null=True)
clientID = models.CharField(max_length=30, unique=True, blank=True, null=True)
prjmanager = models.CharField(max_length=64, unique=False, blank=True, null=True)
acclead = models.CharField(max_length=64, unique=False, blank=True, null=True)
artdirector = models.CharField(max_length=64, unique=False, blank=True, null=True)
prdlead = models.CharField(max_length=64, unique=False, blank=True, null=True)
intlead = models.CharField(max_length=64, unique=False, blank=True, null=True)
prjname = models.CharField(max_length=200, unique=True)
prjstatus = models.CharField(max_length=50, choices=STATUS_CHOICES)
as_of = models.DateField(auto_now_add=False)
format = models.CharField(max_length=64, unique=False, blank=True, null=True)
target_studio = models.DateField(unique=False, blank=True, null=True)
mech_return = models.DateField(unique=False, blank=True, null=True)
comp_return = models.DateField(unique=False, blank=True, null=True)
target_release = models.DateField(unique=False, blank=True, null=True)
record_added = models.DateField(auto_now_add=True)
record_modified = models.DateTimeField()
studio_name = models.CharField(max_length=64, unique=False, blank=True, null=True)
studio_process = models.CharField(max_length=64, unique=False, blank=True, null=True, choices=PROCESS_CHOICES)
to_studio = models.DateTimeField(unique=False, blank=True, null=True)
from_studio = models.DateTimeField(unique=False, blank=True, null=True)
studio_name2 = models.CharField(max_length=64, unique=False, blank=True, null=True)
studio_process2 = models.CharField(max_length=64, unique=False, blank=True, null=True, choices=PROCESS_CHOICES)
to_studio2 = models.DateTimeField(unique=False, blank=True, null=True)
from_studio2 = models.DateTimeField(unique=False, blank=True, null=True)
comments = models.TextField(max_length=500, unique=False, blank=True, null=True)
summary = models.TextField(max_length=500, unique=False, blank=True, null=True)
upload_pdf = models.CharField(max_length=50, unique=False, blank=True, null=True)
upload_achive = models.CharField(max_length=50, unique=False, blank=True, null=True)
def __unicode__(self):
return u'%s' % self.prjname
class Admin:
pass
模型形式“RecordForm”由此衍生而来。