Monday, 20 October 2014

Playing with Data Retrieval from Databases to Templates via Views

Add the following function in views.py,
#from __future__ import unicode_literals
from django.http import HttpResponse
from django.core.context_processors import csrf
from books.models import Books
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.template.loader import get_template
from django.http import HttpResponse

 def retrieve(request):
         Book_info = Books.objects.get(id = ‘12’)
         All_info = Books.objects.filter(author=’priyanka’)
    books_list = Books.objects.all().order_by('-timestamp')
    return render(request,'loggedin.html',{'books_list': books_list,’Book_info’:Book_info,’All_info’:All_info})


Explanation for the above code:

  1. objects.get( [The condition required] )
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
                                                   Book_info = Books.objects.get(id = ‘12’)
Here, we retrieve information of the book  whose id is 12 and it gets stored in the variable Book_info


  1. objects.filter( [The condition required] )

Returns a new QuerySet containing objects that match the given lookup parameters.
                                                  All_info = Books.objects.filter(author=’priyanka’)
Here we  retrieve information of all the books which match the author name  ‘priyanka’.

Note: filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
where as  get() method returns only one  object that matches the query.

  1. objects.all()
Returns all the objects from the table.
                                         books_list = Books.objects.all().order_by('-timestamp')
Here  book_list variable that contains information about all the books stored in the table but ordered according to the timestamp.
                                                    

We can retrieve the data from database tables in various ways.Please do refer https://docs.djangoproject.com/en/1.7/topics/db/queries/   for the same.




  Now that we have retrieved the data from databases, we can post information retrieved on the templates using render.
                                                   return render(request,'2.html',{'books_list': books_list,’Book_info’:Book_info,’All_info’:All_info})

request indicates the template to which we need to pass on the data retrieved.Here we pass on the data retrieved to 2.html whose creation is explained below.



CAUTION: We can use return render_to_response instead of return render, but in the former one helps in posting only one variable at a time but the latter one helps in posting as many as variables/query sets we need.

If one had used return render_to_response one  had to write three different statements instead of one return render.


To use return render add the following header at the top of views.py
                                 from django.shortcuts import render

Add the following piece of code in urls.py  to connect views.py and template(2.html),


                          from django.conf.urls.defaults import *

                           urlpatterns = patterns('',
                                  (r'^retrieve/$', 'books.views.retrieve),
                                  )




Creation of template(2.html)

<html>
<head></head>
<body>
< - - - - ! Posts the detail of the book with ID 12 - - - - >
  {% for b in Book_info %}
    <p> Name: {{b.name}}</p>
    <p> ID: {{b.id}}</p>
   <p> Author: {{b.author}}</p>
  <p>Purchased time: {{b.time_at_which_it_was_bought}}</p>
{% endfor %}

< - - - - ! Posts the detail of all the books whose author name matches with Priyanka- ->
  {% for c in All_info %}
    <p> Name: {{c.name}}</p>
    <p> ID: {{c.id}}</p>
   <p> Author: {{c.author}}</p>
  <p>Purchased time: {{c.time_at_which_it_was_bought}}</p>
{% endfor %}



< - - - - ! Posts the detail of all the books in the table ->
  {% for d in books_list %}
    <p> Name: {{d.name}}</p>
    <p> ID: {{d.id}}</p>
   <p> Author: {{d.author}}</p>
  <p>Purchased time: {{d.time_at_which_it_was_bought}}</p>
{% endfor %}
</body>
</html>

No comments:

Post a Comment