首页 > 解决方案 > 如何修复列表视图未在 Django 中显示

问题描述

如何修复列表视图未在 Django 中显示!

当我看到它没有显示数据时,我在基于类的视图中添加列表视图html file我很累要修复你能帮帮我吗!

这是我的意见.py

class Buy_List_View(ListView):
    model = user_buy
    template_name = 'index.html'

这是我的models.py

class user_buy(models.Model):
    users = models.ForeignKey(user_register_model,on_delete=models.CASCADE)
    payment_method = models.CharField(max_length=500)
    price = models.IntegerField()
    limits = models.IntegerField()

这是我的index.html

{% for buy in user_buy_list %}
    <tr>
        <td><a href="#">{{ buy.users }}</a></td>
        <td>{{ buy.payment_method }}</td>
        <td>{{ buy.price }}</td>
        <td>{{ buy.limits }}</td>
    </tr>
{% endfor %}

这是我的urls.py

from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index,name='index'),
    path('accounts/signup/', views.user_reg,name='register'),
    path('profile/<username>', views.user_profile, name='user_profile'),
    path('buy/form/seller',views.Buy_List_View.as_view(),name='buy')
]

标签: djangodjango-modelsdjango-templatesdjango-viewsdjango-class-based-views

解决方案


我的 urls.py 有问题我想在主页中显示,但我写了个人资料页面,这就是为什么我看不到我需要更改而不是这个

from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.index,name='index'),
    path('accounts/signup/', views.user_reg,name='register'),
    path('profile/<username>', views.user_profile, name='user_profile'),
    path('buy/form/seller',views.Buy_List_View.as_view(),name='buy')
]

至此

from . import views
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.Buy_List_View.as_view(),name='index'),
    path('accounts/signup/', views.user_reg,name='register'),
    path('profile/<username>', views.user_profile, name='user_profile'),
]

因为我想显示在主页而不是这个页面!


推荐阅读