Blog

Django Comes with simple Data query API for the database…

Code below Demonstrated how you can retrieve all blog posts from database


from django.shortcuts import render
from .models import Post

def index(request):
    get_all_posts = Post.objects.all();
    return render(request, 'templates/index.html', {'all_posts' : get_all_posts})

Explaination

The function render() is the shortcut method to create a HttpResponse that is sent back to the browser. It creates an HTML file by combining the corresponding template and data we inserted, i.e., the variable ‘all_posts’. It inserts the data that we provided in the HTML file. You can learn more in the later tutorial.

Next >> Django Tutorial : Passing View data to Template (HTML templates)

Leave a Reply

Your email address will not be published. Required fields are marked *