How to show models in homepage

Hi my friends,
I wanna make simple example in order to understand some basics, so

I made these steps

  • First i create file as a name Prusa
  • Startproject mysite
  • Startapp register
  • runserver and everything is ok
  • i made register/templates file and made settings.py arrangement
  • i created two ile under templates index.html and about.html
  • first i create this model on models.py
from django.db import models

# Create your models here.
class Patient(models.Model):
    name = models.CharField(max_length=100)
    surname = models.CharField(max_length=100)
    ilness = models.CharField(max_length=200)
    
  • views.py i made this codes
from django.shortcuts import render
from django.http import HttpResponse
from .models import Patient



# Create your views here.
def About(request):
    
    return render(request, 'about.html')
 

def Anasayfa(request):
    return render(request, 'index.html')


def Doctor(request):
    post = Patient.object.all()
    return render(request, 'index.html', {'post': post})  
  • register.urls.py i made these codes
from django.urls import path
from .views import About, Anasayfa

urlpatterns = [
    
    path('', Anasayfa, name='index'),
    path('about/', About, name='about'),
]
  • mysite.urls.py i made these codes
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('register.urls')),
]

  • register.admin.py file i made these codes
from django.contrib import admin
from .models import Patient

# Register your models here.

admin.site.register(Patient)

  • templates/index.html i made these codes
<h1>Hello</h1>

<div>
    {% for post in posts %}
    <div>
        <p>{{ post.name }}</p>
       
    </div>
{% endfor %}
</div>

After all when i runserver

i see only hello , but i expect see the patient name,
i created super user and i added 2 patient form django admin panel but, i see nothing except Hello .

How can i see patient names under the Hello word?

You are doing for post in posts in the template, but the view is not passing posts it is passing post, so as far as the template is concerned posts does not exist.

Just change the payload in the view to pass "posts": post and it should be OK.

hi my friend,

    return render(request, 'index.html', {'post': post}) 

i did {‘posts’: post} - runserver nothing change
i did {‘post’: posts} - runserver nothing change
i did {‘posts’: posts} - runserver nothing change

:frowning:

In your view:

post is the collection of Patient objects.

I could have sworn you mapped {"post": post} before - have you edited your view?

No matter, the value of the string "post" there is what you can reference in your template. I suggest you call it posts to show that it can be plural and avoid confusion, though the name is unimportant really.

In your template:

{% for post in posts %}

The posts there is the value you pass in the context. At the moment this is “post”, so this needs to match. post here is a local var you’re creating just for that loop, and contains each instance as you iterate over the collection of Patent objects you’ve sent.

So, either change “post” => “posts” in the view and it should work, or change the template to something like

for p in post

And then reference p.name instead.

You can’t do for post in post as the template won’t know what you’re doing - 2 different vars with the same name.

1 Like

Your home page url appears to be mapped to the Anasayfa view

This view doesn’t render any data.

1 Like

Finally i did :muscle:
Thanks @michjnich and @KenWhitesell

Firstly i did "for p in post " and gets error, and googled and find solution
And secondly i changed the def

def Anasayfa(request):
    post = Patient.objects.all()
    return render(request, 'index.html', {'post': post})

And i get again error. When i search on the net and i understand i need makemigrations and migrate, because i changed the models name…

Anyway, now i have what i want with your supports. Thanks my friends…