如何修复列表视图未在Django中显示!
当我看到html file
中没有显示数据时,我在基于类的视图中添加列表视图,我很累要解决,请您帮我!
这是我的 views.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')
]
答案 0 :(得分:0)
views.py
class Buy_List_View(generic.ListView):
model = user_buy
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
def get_queryset(self):
user_buy = self.model.objects.all()
return user_buy
您必须在模板中更改对象名称
{% for buy in object_list %}
<tr>
<td><a href="#">{{ buy.users }}</a></td>
<td>{{ buy.payment_method }}</td>
<td>{{ buy.price }}</td>
<td>{{ buy.limits }}</td>
</tr>
{% endfor %}
答案 1 :(得分:0)
我的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'),
]
因为我想在主页上显示而不是此页面!