Python 我无法从后端Django检索OneToOneField数据
我可以从用户(例如User.id)的后端获取数据,但当我想从OneToOneField(例如User.checkAdmin.isAdmin)获取数据时,它不会在html页面中显示任何数据 views.pyPython 我无法从后端Django检索OneToOneField数据,python,django,python-3.x,django-2.2,Python,Django,Python 3.x,Django 2.2,我可以从用户(例如User.id)的后端获取数据,但当我想从OneToOneField(例如User.checkAdmin.isAdmin)获取数据时,它不会在html页面中显示任何数据 views.py def registerView(request): if request.method == "POST": form = UserCreationForm(request.POST) isAdminForm = checkAdminForm(requ
def registerView(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
isAdminForm = checkAdminForm(request.POST) #
if form.is_valid() and isAdminForm.is_valid():
user = form.save()
isAdminFormObject = isAdminForm.save(commit=False)
isAdminFormObject.user = user
isAdminFormObject.save()
return redirect('login_url')
else:
form = UserCreationForm()
isAdminForm = checkAdminForm()
return render(request, 'registration/register.html',{'form':form,'isAdminForm':isAdminForm})
Forms.py
from django import forms
from .models import checkAdmin
class checkAdminForm(forms.ModelForm):
class Meta:
model = checkAdmin
fields = ['isAdmin']
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class checkAdmin(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
isAdmin = models.BooleanField(default=False)
def __str__(self):
return self.user.username
dashboard.html
{% extends 'index.html' %}
{% block content %}
<h1>Welcome, {{ user.id }}</h1>
<h1>check?,{{ user.checkAdmin.isAdmin }}</h1>
<button>Mark Attendance</button> <button>Mark Leave</button> <button>View Attance</button>
<a href="{% url 'logout' %}">Logout</a>
{% endblock %}
{%extends'index.html%}
{%block content%}
欢迎,{{user.id}
检查?,{user.checkAdmin.isAdmin}
标记考勤标记休假视图附件
{%endblock%}
使用checkadmin而不是checkadmin
{% extends 'base.html' %}
{% block content %}
<h1>Welcome, {{ user.id }}</h1>
<h1>check?,{{ user.checkadmin.isAdmin }}</h1>
<button>Mark Attendance</button> <button>Mark Leave</button> <button>View Attance</button>
<a href="">Logout</a>
{% endblock %}
{%extends'base.html%}
{%block content%}
欢迎,{{user.id}
检查?,{user.checkadmin.isAdmin}
标记考勤标记休假视图附件
{%endblock%}
非常感谢,先生。。它起作用了。。请告诉我为什么这个用小写字母写,而不是我在model.py类名中写的checkAdmin?类名在Django模板变量中转换为小写字母。我不知道确切的原因。请将答案标记为已接受