12(c). ok now we will work on models in django

in the contact app

views.py


from django.views.generic import ListView
from contacts.models import Contact

class ListContactView(ListView):
      model=Contact




For our purposes, however, we don’t need that extra layer of directory structure, so we’ll specify the template to use explicitly, using the template_name property. Let’s add that one line to views.py.
from django.views.generic import ListView

from contacts.models import Contact


class ListContactView(ListView):

    model = Contact
    template_name = 'contact_list.html'
Create a templates subdirectory in our contacts application, and create contact_list.html there.
<h1>Contacts</h1>

<ul>
  {% for contact in object_list %}
    <li class="contact">{{ contact }}</li>
  {% endfor %}
</ul>
Opening the page in the browser, we should see one contact there, the one we added earlier through the interactive shell.





referesh the page and the output:
























so we saw that how to create model and how to view with the app from the database......




No comments:

Post a Comment