Advertisement
Larrisa

Book site

Apr 26th, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. ##Model
  2. class Book(models.Model):
  3.     """
  4.    Model for Books stored
  5.    variables:
  6.        id - pk autogenerated
  7.        Author - Writer of said book
  8.        isbn - unique identifier for each book.
  9.        Title - {str} Name of book
  10.        price - {float} - book price
  11.        Available- {Boolean} If book is up for request
  12.    """
  13.     title = models.CharField(max_length=300, blank=False, null=False)
  14.     author = models.CharField(max_length=200)
  15.     isbn = models.CharField(max_length=200, blank=False, null=False)
  16.     price = models.FloatField()
  17.     available = models.BooleanField(default=True)
  18.     published_by = models.ForeignKey(Publisher, blank=True, null=True, \
  19.         on_delete=models.CASCADE)
  20.     synopsis = models.TextField()
  21.  
  22.     def __str__(self):
  23.         author = 'Author needs updating'
  24.         if self.author:
  25.             return '{} - {}'.format(self.title, self.author)
  26.         else:
  27.             return '{} - {}'.format(self.title, author)
  28.  
  29. ##View
  30. def book_detail(request, id):
  31.     """book detailed views"""
  32.     book = Book.objects.get(id=id)
  33.  
  34.     context = {
  35.         'title': book.title,
  36.         'author': book.author,
  37.         'isbn': book.isbn,
  38.         'price': book.price,
  39.         'available': book.available,
  40.         'synopsis': book.synopsis
  41.     }
  42.  
  43.     return render(request, 'library/book_detail.html', context=context)
  44.  
  45.  
  46. ##template Books list
  47. {% extends 'base.html' %}
  48.  
  49. {% block content %}
  50.     <div class="list-group">
  51.  
  52.     {% for book in books %}
  53.         <a href="{% url 'book-details' %}" class="list-group-item list-group-item-action
  54.             flex-column align-items-start active">
  55.  
  56.             <div class="d-flex w-100 justify-content-between">
  57.              <h5 class="mb-1">{{ book.title | title}}</h5>
  58.             <small> {{ book.author }} </small>
  59.              </div>
  60.             <p class="mb-1">
  61.             {{ book.synopsis | truncatewords:"60"}}
  62.             </small>
  63.         </a>
  64.  
  65.     {% endfor %}
  66.  
  67. {% endblock content %}
  68.  
  69. ##Template book detail
  70. {% extends 'base.html' %}
  71.  
  72. {% block content %}
  73.     <div>
  74.         <h2>{{title}} </h2> <br>
  75.         <p> Author: {{author}}</p><br>
  76.         <p>price: {{price}}</p>  <br>
  77.         <p>available: {{ available }}</p>
  78.  
  79.     </div>
  80. {% endblock content %}
  81.  
  82. ##Urls
  83.  
  84. #Urls for the library app
  85.  
  86. from django.urls import path
  87.  
  88. from .views import books, book_detail, BookDetailedView
  89.  
  90. urlpatterns = [
  91.     path('books/', books, name='all_books'),
  92.     path('book/<int:id>/details/', book_detail, name='book-details'),
  93.  
  94. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement