我有一个表单,我想根据当前用户更新值,但是当我点击提交时,我无法更新表单。目前,值未更新
这是我的views.py
@login_required(login_url='/')
def student_update(request):
#return UpdateView(request, form_class=UpdateStudentForm, object_id=request.user, template_name='success.htm')
form = UpdateStudentForm(request.POST, instance=request.user)
if request.method == 'POST':
if form.is_valid():
m = form.save(commit=False)
m.user = request.user
m.save()
return HttpResponseRedirect('/success.html')
else:
form = UpdateStudentForm(instance=request.user)
return render(request, "famu/profile.html", {'form': form})
my forms.py
class UpdateStudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ('street_number', 'street_name', 'apt_number', 'city','state','zipcode')
#def save(self, commit=True):
# user = super(UpdateStudentForm, self).save(commit=False)
# user.state = self.cleaned_data['state']
# if commit:
# user.save()
# return user
我的model.py包含以下代码
from django.db import models
from django.contrib.auth.models import User
class Student(models.Model):
CLASSIFICATION = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
)
TERMS = (
('F', 'Fall'),
('S', 'Spring'),
('Su', 'Summer'),
)
MAJOR = (
('CS', 'Computer Science'),
('CY', 'Computer Systems'),
('IT', 'Information Technology'),
)
username = models.OneToOneField(User)
student_id = models.IntegerField(primary_key=True, max_length=9)
classification = models.CharField(max_length=2, choices=CLASSIFICATION)
enrolled_year = models.IntegerField(max_length=4)
enrolled_term = models.CharField(max_length=2, choices=TERMS)
major = models.CharField(max_length=2, choices=MAJOR)
street_number = models.IntegerField(max_length=10)
street_name = models.CharField(max_length=45)
apt_number = models.CharField('apt #', max_length=10, blank=True, )
city = models.CharField(max_length=30)
state = models.CharField(max_length=2)
zipcode = models.IntegerField('Zip Code', max_length=5, )
last_modified = models.DateTimeField(auto_now=True)
class Grade (models.Model):
GRADES = (
('A', 'A'),
('B', 'B'),
('C', 'C'),
('D', 'D'),
('F', 'F'),
('T', 'Transfer'),
)
TERMS = (
('F', 'Fall'),
('S', 'Spring'),
('Su', 'Summer'),
)
CREDITS = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
(6, '6'),
(7, '7'),
(8, '8'),
(9, '9'),
)
student_id = models.ForeignKey(Student)
course_prefix = models.CharField(max_length=3)
course_number = models.CharField(max_length=5)
grade= models.CharField(max_length=1, choices=GRADES)
term_taken = models.CharField(max_length=2, choices=TERMS)
year_taken = models.IntegerField(max_length=4)
credits = models.IntegerField(max_length=1, choices=CREDITS)
class Message (models.Model):
sender = models.ForeignKey(User, related_name='sender')
receiver = models.ForeignKey(User, related_name='receiver')
message_content = models.CharField(max_length=1000)
class Gradapp (models.Model):
student_id = models.OneToOneField(Student, primary_key=True)
submitted = models.BooleanField(default=False)
last_modified = models.DateTimeField(auto_now=True)
答案 0 :(得分:0)
根据你的models.py,views.py中的这一行:
m.user = request.user
应该是
m.username = request.user
答案 1 :(得分:0)
request.POST包含表单数据,但是如果你在顶部实例化表单,那么它也可能是一个空的QueryDict,如果它是一个GET。这很可能导致问题,如果是GET,你应该传递None。试试这个:
form = UpdateStudentForm(request.POST or None, instance=request.user)
请注意,这也会清理您的代码,因为您不需要为GET创建另一个表单:
form = UpdateStudentForm(request.POST or None, instance=request.user)
if request.method == 'POST':
if form.is_valid():
m = form.save(commit=False)
m.user = request.user
m.save()
return HttpResponseRedirect('/success.html')
return render(request, "famu/profile.html", {'form': form})