Python 使用 django 在 HTML 页面中显示数据库中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36631419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display data from a database in a HTML page using django
提问by Luke Kavanagh
I am trying to add data to a html page using python but the page is blank apart from the normal elements on the page.
我正在尝试使用 python 将数据添加到 html 页面,但除了页面上的正常元素之外,该页面是空白的。
this is my views.py
:
这是我的views.py
:
def index(request):
next = request.GET.get('next', '/admin')
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
data = Students.objects.all()
return render_to_response("login/profile.html", {'data', data})
else:
HttpResponse("Inactive User.")
else:
print("User Not Found!")
return HttpResponseRedirect(settings.LOGIN_URL)
return render(request, 'login/home', {'redirect_to':next})
this is my student module :
这是我的学生模块:
class Students(models.Model):
student_number = models.IntegerField(primary_key=True)
f_name = models.CharField(max_length=20, blank=True, null=True)
l_name = models.CharField(max_length=20, blank=True, null=True)
dob = models.DateField(blank=True, null=True)
address = models.CharField(max_length=144, blank=True, null=True)
county = models.CharField(max_length=20, blank=True, null=True)
phone_number = models.CharField(max_length=45, blank=True, null=True)
email = models.CharField(max_length=45, blank=True, null=True)
gpa = models.IntegerField(blank=True, null=True)
course_code = models.ForeignKey(Courses, models.DO_NOTHING, db_column='course_code', blank=True, null=True)
college = models.ForeignKey(Colleges, models.DO_NOTHING, blank=True, null=True)
passwords = models.CharField(max_length=60, blank=True, null=True)
class Meta:
db_table = 'students'
my html page :
我的 html 页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>HEY!!!!</h1>
<p>
{{ data.f_name}}
{{ data.l_name }}
{{ data.student_number }}
{{ data.dob }}
</p>
<a href="/">logout</a>
</body>
</html>
I have been looking this up for a while but I can't seem to find why this isn't displaying anything on my page. If anyone could help I'd appreciate it!
我已经查找了一段时间,但我似乎无法找到为什么我的页面上没有显示任何内容。如果有人可以帮助我将不胜感激!
回答by Luke Kavanagh
It took me a while but I think that I have finally found an answer for my question.
我花了一段时间,但我想我终于找到了我的问题的答案。
adding this in to the views.py
将此添加到 views.py
data = Students.objects.all()
stu = {
"student_number": data
}
return render_to_response("login/profile.html", stu)
then it iterate through the data that is stored in the stu
variable
然后它遍历存储在stu
变量中的数据
{% for student in student_number %}
{{ student.f_name}}
{{ student.l_name }}
{{ student.student_number }}
{{ student.dob }}
{% endfor %}
回答by Mad Wombat
In your context, you pass Student.objects.all() which is a list of students. And then treat is as a single object in the template. You need to iterate over your list.
在您的上下文中,您传递 Student.objects.all() 这是一个学生列表。然后将 is 视为模板中的单个对象。您需要遍历您的列表。
回答by Shang Wang
Another possible cause is that when your request.method
is not POST
(should be GET
), you were not passing data
in the context. You returned:
另一个可能的原因是,当您request.method
不是POST
(应该是GET
)时,您没有data
在上下文中传递。你回来了:
return render(request, 'login/home', {'redirect_to':next})
instead. Django template won't give you error when you have undefined variable, but it won't show anything either.
反而。当您有未定义的变量时,Django 模板不会给您错误,但它也不会显示任何内容。