Print data from Database in Django

13 views
Skip to first unread message

anchal agarwal

unread,
May 5, 2019, 2:10:43 PM5/5/19
to django...@googlegroups.com
I want to display the name of all the students from the Student model using cards , i have set it all up but it isn't displaying anything, here are my files  

models.py

from django.db import models
from django.utils import timezone
from datetime import date
from ckeditor.fields import RichTextField

class Student(models.Model):
full_name=models.CharField(max_length=70)
D_O_B=models.TextField()
def _str_(self):
return self.full_name

class Article(models.Model):
pub_date=models.DateField(default=timezone.now())
headline=models.CharField(max_length=70)
content=RichTextField()
reporter=models.ForeignKey(Student,on_delete=models.CASCADE)


urls.py

from django.urls import path
from . import views

app_name="students"

urlpatterns=[
path('Students.html/',views.homepage,name='homepage'),
path("<str:full_name>/",views.detail,name='detail'), ]


views.py 

from django.shortcuts import render
from django.http import HttpResponse
from .models import Student

def homepage(request):
context={"stud": Student.objects.all}
print(Student.objects.all)
return render(request,"students/Students.html",context)

def detail(request,full_name):
return HttpResponse("You are looking %s!!" % full_name)

Students.html
<head>
<!-- Compiled and minified CSS -->
<!-- Compiled and minified JavaScript -->
</head>
<body>
<div class="row">
{% for stud in Student %}
<div class="col s12 m6">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title">{{stud.full_name}}</span>
<p>{{stud.D_O_B}}</p>
</div>
<div class="card-action">
<a href="#">This is a link</a>
<a href="#">This is a link</a>
</div>
</div>
</div>
{% endfor %}
</div>

</body>

Thomas Lockhart

unread,
May 5, 2019, 2:13:32 PM5/5/19
to django...@googlegroups.com
Your context variable for all students is “stud”. Try changing that to “Student” as you refer to it in your template.

- Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMT%3DisXCa7Yc-vM3%2B5c0jWdCX8hgwJFN2tRtCqkK2NbzzqX-nA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

anchal agarwal

unread,
May 5, 2019, 2:34:29 PM5/5/19
to django...@googlegroups.com

Thanks alot , it worked !!

- Tom

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages