render_to edit_record html def edit_record request id record get_objec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@render_to("edit_record.html")
def edit_record(request, id):
record = get_object_or_404(Record, pk=id)
if request.method == 'POST':
form = EditRecord(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect("/testme/")
else:
return {'form': form}
if record.user == request.user:
initial = {'user': record.user, 'institution': record.institution}
form = EditRecord(initial=initial, instance=record)
return {'form': form}
else:
return HttpResponseRedirect("/")
#forms
class EditRecord(forms.ModelForm):
class Meta:
model = Record
exclude = ('user', 'institution')
#models
class Record(models.Model):
user = models.ForeignKey(User)
institution = models.ForeignKey(Institution)
start_year = models.CharField(max_length=30)
end_year = models.CharField(max_length=30)