我正在尝试为学生班级创建一个 ModelForm。这使用当前登录的用户作为 Django 的身份验证系统和我的模型之间的一对一关系。我无法验证这一点以挽救我的生命。以下是相关型号:
class student(models.Model):
user_id = models.OneToOneField(User,unique=True)
first_name = models.CharField(max_length=45)
last_name = models.CharField(max_length=45)
#enrolled_classes = models.ManyToManyField('agility_class')
#completed_classes = models.ManyToManyField('agility_class')
email_address = models.EmailField()
phone_number = PhoneNumberField()
training_experience = models.CharField(max_length=500)
#training_materials =
def __unicode__(self):
return self.first_name + " " + self.last_name;
class studentForm(ModelForm):
class Meta:
model = student
我的观点是这样的:
def profile_creation(request):
if request.method == 'POST':
thisform = studentForm(request.POST)
if thisform.is_valid():
thisform.save()
return HttpResponseRedirect('/about')
return render(request, 'profile_registration.html')
这是我模板中的表单的样子:
<form class="form-horizontal" method = 'POST'>
{%csrf_token%}
<fieldset>
<!-- Form Name -->
<legend>Profile</legend>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="first_name">First Name</label>
<div class="controls">
<input id="first_name" name="first_name" type="text" placeholder="" class="input-large">
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="last_name">Last Name</label>
<div class="controls">
<input id="last_name" name="last_name" type="text" placeholder="" class="input-large">
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input id="email_address" name="email_address" type="text" placeholder="" class="input-large">
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="phone_number">Phone Number</label>
<div class="controls">
<input id="phone_number" name="phone_number" type="text" placeholder="" class="input-large">
<p class="help-block">Format: 123-456-7890</p>
</div>
</div>
<!-- Textarea -->
<div class="control-group">
<label class="control-label" for="training_experience">Training Experience</label>
<div class="controls">
<textarea id="training_experience" name="training_experience"></textarea>
</div>
</div>
<input type="hidden" name= "user_id" value ="{{ user.username }}">
<input type="submit" value="submit">
</fieldset>
</form>
也许我错过了一些激烈的东西......提前感谢您的帮助。