class locations(models.Model):#table
Name = models.CharField(max_length=50,default='Joes quick stop', unique=True)
shop_code = models.CharField(max_length=5,default='AB005',unique=True)
manager = models.ManyToManyField(users)
ast_manager = models.ManyToManyField(users, blank=True, related_name='ast_mng')
sales_manager = models.ManyToManyField(users, blank=True, related_name='sales_mng')
形式:
class locations(models.Model):
Name = models.CharField(max_length=50,default='Joes quick stop', unique=True)
shop_code = models.CharField(max_length=5,default='AB005',unique=True)
manager = models.ManyToManyField(users)
ast_manager = models.ManyToManyField(users, blank=True, related_name='ast_mng')
sales_manager = models.ManyToManyField(users, blank=True, related_name='sales_mng')
class locationsForm(ModelForm):
class Meta:
model = locations
查看
def locations(request):
locations = locations.objects.values().filter(id=request.session['location'])
data=locations[0]
if request.method == 'GET':
if request.session['Manager']== True:
form = locationsForm(initial=data)
context = {'locations': locations, 'form': form}
return render(request, 'locations/locations.html', context)
elif request.method == 'POST':
form=locationsForm(request.POST, instance=data)
if form.is_valid():
cd=form.cleaned_data
form.save()
form = locationsForm()
locations= locations.objects.values().filter(id=request.session['depot'])
context = {'locations': locations}
return render(request, 'locations/locations.html', context)
else:
context = {'locations': locations, 'form': form}
return render(request, 'locations/locations.html', context)
我想顯示填充了一個表格相關數據但用戶可以更改並保存/更新表單。上面的代碼在用相關數據顯示錶單時做得很好,但是當用戶試圖提交時,系統會嘗試保存新記錄而不是更新舊記錄並失敗。我從來沒有過去if form.is_valid():
表單上的錯誤是什麼?你可以用''{{form.errors}}''' – schillingt 2014-11-25 00:10:31
來查看你的模板。錯誤是「這個名稱的位置已經存在」。每個需要唯一值的字段都會顯示錯誤。這就是爲什麼我想它是試圖創造一個新的記錄,而不是更新舊的。 – Jay42 2014-11-25 00:27:16