显示index.html原始路径上的Django DB值



我有一个DB,其中有一些值。我想在index.html上显示这些值。我想在标准html路径path('', views.index, name = "index"),

上显示它们我目前在path('this_is_the_path/', my_db_class.as_view()),

显示它们问题是我正在从类中提取值。我不知道如何将类设置为原始的index.html路径。

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>

所以我的问题是,上面的代码将只显示我的DB值在this_is_the_path路径,但我想显示它在''路径。

像这样修改你的索引函数
views.py

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

相关内容