6

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!

3 Answers 3

7

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 %}
Sign up to request clarification or add additional context in comments.

1 Comment

doing exactly same thing and getting results from db but whne i do page refresh data do not populates form db
2

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.

4 Comments

I have added this {% for ---- in ---- %} but I'm not sure what I should put in in place of the hyphens?
{% for student in data %}
Thanks for helping but I'm still only getting a blank page
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.