Reputation: 229
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.
this is my 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 :
<!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!
Upvotes: 6
Views: 51810
Reputation: 229
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
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
{% for student in student_number %}
{{ student.f_name}}
{{ student.l_name }}
{{ student.student_number }}
{{ student.dob }}
{% endfor %}
Upvotes: 7
Reputation: 25559
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:
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.
Upvotes: 0
Reputation: 15155
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.
Upvotes: 2