# How to show models in homepage

**URL:** <https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279>\
**Category:** Using Django\
**Created:** [August 18, 2021, 2:32pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279 "2021-08-18T14:32:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prusaseo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/prusaseo/32/5406_2.png) [@Prusaseo](https://forum.djangoproject.com/u/Prusaseo)\
**Post date:** [August 18, 2021, 2:32pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/1 "2021-08-18T14:32:42Z")

</div>

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

```auto
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

```auto
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

```auto
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

```auto
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

```auto
from django.contrib import admin
from .models import Patient

# Register your models here.

admin.site.register(Patient)

```

- templates/index.html i made these codes

```auto
<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?

 ![hello](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/f/f7d5915cb2ff4a11fcf470370c668b85c11128fb.jpeg)

---

<div class="post-metadata">

**Author:** ![michjnich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/michjnich/32/2424_2.png) [@michjnich](https://forum.djangoproject.com/u/michjnich)\
**Post date:** [August 18, 2021, 3:15pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/2 "2021-08-18T15:15:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Prusaseo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/prusaseo/32/5406_2.png) [@Prusaseo](https://forum.djangoproject.com/u/Prusaseo)\
**Post date:** [August 18, 2021, 3:51pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/3 "2021-08-18T15:51:47Z")

</div>

hi my friend,

```auto
    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

☹

---

<div class="post-metadata">

**Author:** ![michjnich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/michjnich/32/2424_2.png) [@michjnich](https://forum.djangoproject.com/u/michjnich)\
**Post date:** [August 18, 2021, 4:54pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/4 "2021-08-18T16:54:32Z")

</div>

In your view:

> [@Prusaseo](#):
>
> ```auto
> def Doctor(request):
> post = Patient.object.all()
> return render(request, 'index.html', {'post': post})  
> 
> ```

`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:

```auto
{% 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

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 18, 2021, 4:59pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/5 "2021-08-18T16:59:36Z")

</div>

> [@Prusaseo](#):
>
> ` path('', Anasayfa, name='index'),`

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

> [@Prusaseo](#):
>
> ```auto
> def Anasayfa(request):
> return render(request, 'index.html')
> 
> ```

This view doesn’t render any data.

---

<div class="post-metadata">

**Author:** ![Prusaseo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/prusaseo/32/5406_2.png) [@Prusaseo](https://forum.djangoproject.com/u/Prusaseo)\
**Post date:** [August 18, 2021, 6:02pm UTC](https://forum.djangoproject.com/t/how-to-show-models-in-homepage/9279/6 "2021-08-18T18:02:07Z")

</div>

Finally i did 💪  
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

```auto
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…

 ![patientname](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/2/28cddfb2f48fe4dd27d3c2f42902a1aa56167274.jpeg)
