Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Model
- class Book(models.Model):
- """
- Model for Books stored
- variables:
- id - pk autogenerated
- Author - Writer of said book
- isbn - unique identifier for each book.
- Title - {str} Name of book
- price - {float} - book price
- Available- {Boolean} If book is up for request
- """
- title = models.CharField(max_length=300, blank=False, null=False)
- author = models.CharField(max_length=200)
- isbn = models.CharField(max_length=200, blank=False, null=False)
- price = models.FloatField()
- available = models.BooleanField(default=True)
- published_by = models.ForeignKey(Publisher, blank=True, null=True, \
- on_delete=models.CASCADE)
- synopsis = models.TextField()
- def __str__(self):
- author = 'Author needs updating'
- if self.author:
- return '{} - {}'.format(self.title, self.author)
- else:
- return '{} - {}'.format(self.title, author)
- ##View
- def book_detail(request, id):
- """book detailed views"""
- book = Book.objects.get(id=id)
- context = {
- 'title': book.title,
- 'author': book.author,
- 'isbn': book.isbn,
- 'price': book.price,
- 'available': book.available,
- 'synopsis': book.synopsis
- }
- return render(request, 'library/book_detail.html', context=context)
- ##template Books list
- {% extends 'base.html' %}
- {% block content %}
- <div class="list-group">
- {% for book in books %}
- <a href="{% url 'book-details' %}" class="list-group-item list-group-item-action
- flex-column align-items-start active">
- <div class="d-flex w-100 justify-content-between">
- <h5 class="mb-1">{{ book.title | title}}</h5>
- <small> {{ book.author }} </small>
- </div>
- <p class="mb-1">
- {{ book.synopsis | truncatewords:"60"}}
- </small>
- </a>
- {% endfor %}
- {% endblock content %}
- ##Template book detail
- {% extends 'base.html' %}
- {% block content %}
- <div>
- <h2>{{title}} </h2> <br>
- <p> Author: {{author}}</p><br>
- <p>price: {{price}}</p> <br>
- <p>available: {{ available }}</p>
- </div>
- {% endblock content %}
- ##Urls
- #Urls for the library app
- from django.urls import path
- from .views import books, book_detail, BookDetailedView
- urlpatterns = [
- path('books/', books, name='all_books'),
- path('book/<int:id>/details/', book_detail, name='book-details'),
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement