amr_aly

book_borrow_app

Jul 20th, 2021 (edited)
940
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ############## in views.py
  2. from django.shortcuts import render, redirect
  3. from django.urls import reverse
  4. from django.contrib import messages
  5. from .models import *
  6. from datetime import date
  7.  
  8.  
  9. ## for delete any record in database
  10. def delete_book(request, id):
  11.     ''' Delete Record from Database'''
  12.     qs = Your_Model.objects.get(id=id)
  13.     del_book = qs.delete()
  14.    
  15.     return redirect(reverse('book:extend_book_borrow'), kwargs={'id':id})#('/')  
  16.     # or you can redirect to any url you want
  17.  
  18.  
  19. def extend_book_borrow(request, book_id):
  20.     ''' '''
  21.     book  = your_book_model.objects.get(id=book_id)
  22.     today = date.today()
  23.     update_expirydate = Your_Model.objects.filter(id=book_id)\
  24.                         .update(expirydate__year=today.year, expirydate__month=today.month, expirydate__day=((today.day)+5))  ## +5 days or +7 days
  25.    
  26.     context = {'instance': book,
  27.             'book_id': book_id,        
  28.     }
  29.     return redirect(request, 'your_templates.html', context)  
  30.  
  31. ############## in urls.py
  32.  
  33. from .views import delete_book
  34.  
  35. app_name = 'book'
  36. urlpatterns = [
  37.     path('delete/book/<int:id>/', delete_book, name='delete_book'),
  38.     path('extend/book/id/<int:book_id>/', extend_book_borrow, name='extend_book_borrow'),
  39. ]
  40.  
  41. #################### in your_templates.html
  42.  
  43. <div class="container">
  44.             <a class="btn btn-outline-danger" href="{% url 'book:delete_book' instance.id %}">
  45.             Delete
  46.             </a>
  47.             <a class="btn btn-outline-success" href="{% url 'book:extend_book_borrow' book_id %}">
  48.             Extend Book Borrow
  49.             </a>
  50. </div>
  51.  
  52.  
  53.  
  54.  
RAW Paste Data