Home > OS >  Displaying Django DB values on index.html original path
Displaying Django DB values on index.html original path

Time:10-01

I have a DB hat hs some values in it. I want to display these values on my index.html. i want to display them on the standard html path path('', views.index, name = "index"),

I currently displaying them at path('this_is_the_path/', my_db_class.as_view()),

The problem is that i am pulling the values from a class. and i can't figure out how to set the class to the original index.html path.

Models.py

from django.db import models

class Database(models.Model):
    text = models.TextField()

Views.py

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Database

# Create your views here.
def index(request):
    return render(request,"index.html",{})

class my_db_class(ListView):
    model = Database
    template_name = 'index.html'

URLS.PY

from django.urls import path
from . import views
from .views import my_db_class


urlpatterns = [
    path('', views.index, name = "index"),
    path('this_is_the_path/', my_db_class.as_view()),
]

HTML.PY

<ul>
    {% for post in object_list %}
    <li> {{ post.text }}</li>
    {% endfor %}
</ul>

So my problem is that the above code will only show my DB values at the this_is_the_path path, but i want to show it at the '' path.

CodePudding user response:

just modify your index function like this.
views.py

def index(request):
    object_list = Database.objects.all()
    return render(request,"index.html",{'object_list':object_list})
  • Related