Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Django Simple Coding Notesbook View

Django Simple coding notes

Uploaded by

Megha basvaraju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Django Simple Coding Notesbook View

Django Simple coding notes

Uploaded by

Megha basvaraju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

a simple Django project that uses a model to store book information, including the book name,

author, title, and description. We will then display the list of books in an HTML page using a for
loop.

Steps:

1. Create the Book Model.


2. Set Up the View to Retrieve Books.
3. Create a Template to Display the Books.
4. Con gure URL Routing.

1. models.py (in your app, e.g., booksapp):

De ne the Book model with elds for name, author, title, and description.

from django.db import models

class Book(models.Model):

book_name = models.CharField(max_length=255)

author = models.CharField(max_length=255)

title = models.CharField(max_length=255)

description = models.TextField()

def __str__(self):

return self.title

2. Create the Database Table:

Run the following commands to create the database table for the Book model:

python manage.py makemigrations

python manage.py migrate

views.py (in your app):


Create a view that retrieves all books from the database.

from django.shortcuts import render

from .models import Book


fi
fi
fi
def book_list(request):

books = Book.objects.all() # Retrieve all books from the database

return render(request, 'booksapp/book_list.html', {'books': books})

urls.py (in your app):


Set up the URL pattern for the book list view.

from django.urls import path


from . import views

urlpatterns = [
path('books/', views.book_list, name='book_list'),
]

6. book_list.html (in templates/booksapp/):

Create an HTML template to display the list of books.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Book List</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet">

</head>

<body>

<div class="container mt-5">

<h1 class="text-center">Book List</h1>

<ul class="list-group mt-3">

{% for book in books %}

<li class="list-group-item">
<h5>{{ book.title }}</h5>

<p><strong>Author:</strong> {{ book.author }}</p>

<p><strong>Description:</strong> {{ book.description }}</p>

</li>

{% empty %}

<li class="list-group-item">No books available.</li>

{% endfor %}

</ul>

</div>

</body>

</html>

7. Add Sample Data:

You can use Django's admin panel or the Django shell to add sample book data. To do this via the
shell:

python manage.py shell

from booksapp.models import Book

# Create some sample books

Book.objects.create(book_name="Book One", author="Author A", title="First Book",


description="This is the rst book.")

Book.objects.create(book_name="Book Two", author="Author B", title="Second Book",


description="This is the second book.")

8. Run the Server:

Run the Django server to view the book list.

python manage.py runserver


fi

You might also like