Hamza Lachi
Hamza Lachi

Reputation: 1064

How To Fix List View is not Showing in Django

How To Fix List View is not Showing in Django!

I Add The List View in Class Based Views When i See It's Not Showing data in html file I'm tired To Fix Can You Please Help Me!

Here is my views.py

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

Here is my 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()

Here is my 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 %}

Here is my 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')
]

Upvotes: 1

Views: 1462

Answers (2)

Mukul Kumar
Mukul Kumar

Reputation: 2103

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

You have to change object name in your template

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

Upvotes: 0

Hamza Lachi
Hamza Lachi

Reputation: 1064

There is problem in my urls.py I want to show in homepage but I write profile page that's why i can't see I need to to change instead of this

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')
]

To This

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'),
]

Because I want to show in home page not this page!

Upvotes: 1

Related Questions