我尝试在我的应用程序上添加一个页面,用户可以在其中看到他们提供给应用程序的first_name,last_name,username和email。到目前为止,我已经让应用程序显示用户名和电子邮件,但是first_name和last_name不会显示,除非我在管理站点的相应字段中手动输入它们。
学生模特:
class Student(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=200, null=True)
last_name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
views.py:
的片段def get_personal_information(request, username):
user = Student.objects.get(username=username)
return render(request, 'polls/personal_information.html', {"user":user})
def create_account(request):
if request.method == 'POST':
new_user = User(username = request.POST["username"], email=request.POST["email"], first_name=request.POST["first_name"], last_name=request.POST["last_name"])
new_user.set_password(request.POST["password"])
new_user.save()
Student.objects.create(user=new_user, first_name=str(request.POST.get("first_name")), last_name=str(request.POST.get("last_name")), email=str(request.POST.get("email")))
new_user.is_active = True
new_user.save()
return redirect('../')
else:
return render(request, 'polls/create_account.html')
admin.py小部件:
class StudentInline(admin.StackedInline):
model = Student
can_delete = False
verbose_name_plural = 'student'
class Studentadmin(admin.ModelAdmin):
list_display = ('username', 'email', 'first_name', 'last_name')
inlines = [StudentInline]
关于它为什么不显示first_name和last_name的任何想法?
编辑:
<form method="post" action="">
{% csrf_token %}
<label for="username">First name:</label><input type="text" name="first_name" id="first_name" required>
<br><br>
<label for="username">Last name:</label><input type="text" name="last_name" id="last_name" required>
<br><br>
<label for="username">Username:</label><input type="text" name="username" id="username" required>
<br><br>
<label for="email">Email Address:</label><input type="text" name="email" id="email" required>
<br><br>
<label for="password">Password:</label><input type="password" name="password" id="password" required>
<br><br>
<label for="confirm">Confirm Password:</label><input type="password" name="password" id="confirm" required>
<input type="submit" value="Create new user account" />
</form>
编辑: 在first_name和last_name表单中,我忘了更改&#34; for = username&#34;当我复制并粘贴它。解决!