简体   繁体   中英

Couldn't list the stuffs from the models to HTML page in Django

Couldn't connect the list view to HTML file in Django.

models.py

class AccountList(models.Model):
name=models.CharField(max_length=200)
paltform=models.CharField(max_length=200)
username=models.CharField(max_length=200,null=True,blank=True)
password=models.CharField(max_length=200)
comments=models.TextField()
def get_absolute_url(self):
    return reverse("accdetail",kwargs={'pk':self.pk})
def __str__(self):
    return self.name

views.py

class Account_list(ListView):
model=AccountList
def get_queryset(self):
    return AccountList.objects.all()

AccountList_list.html

{% extends 'PasswordSafe/base.html' %}
{% block content  %}
<div class="centerstage">
{% for acc in acc_list %}
<h1><a href="{% url 'accdetail' pk=post.pk %}">{{ acc.name }}</a></h1>
{% endfor %}
</div>
{% endblock %}

urls.py

from django.conf.urls import url
from . import views
urlpatterns=[url(r'^$',views.HomePageView.as_view(),name="hmpage"),
url(r'^accountlist/',views.Account_list.as_view(),name="acc_list"),
url(r'^newsafe/',views.NewSafe.as_view(),name="new_safe"),
url(r'^accdetail/(?P<pk>\d+)$',views.AccDetail.as_view(),name="accdetail"),
url(r'^about/',views.AboutPage.as_view(),name="about")]

I Don't Know where did i go wrong.

Try if this works for you.

class Account_list(ListView):
    model = AccountList
    template_name = 'AccountList_list.html'

    def get_context_data(self, *args, **kwargs):
        acc_list = AccountList.objects.all()
        context = super(Account_list,self).get_context_data(*args, **kwargs)
        context['acc_list'] = acc_list
        return context

You need to change acc_list to object_list in html page OR you can set context_object_name = 'acc_list' in views for more info check this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM