A library app created with Python/Django. Users are able to view books within the library, add new books, as well as borrow and return them.
This project was created by @ecarlos09 and @roselynle as our first attempt to create a Django app
To run the code:
- Fork and clone this repo
- cd into repo
- Create a .env file in the root directory and add the following into the file SECRET_KEY = "XYZ", where XYZ can be any string containing a mixture of numbers, letters, and symbols
- Install requirements (optional)
pipenv install -r requirements.txt
- Activate your virtual environment
pipenv shell
- Run the following scripts to for different purposes
- Run server:
pipenv run dev
orcd borrowers
thenpython manage.py runserver
- Run all tests:
pipenv run test
orcd borrowers
thenpython manage.py test borrowers
- Run individual app tests:
- Library:
pipenv run testlib
orcd borrowers
thenpython manage.py testlib
- Users:
pipenv run testusers
orcd borrowers
thenpython manage.py testusers
- Library:
- Run coverage:
- Initialise:
pipenv run initcov
- View report:
pipenv run viewcov
- Initialise:
- Run server:
- See the app in action at
http://localhost:8000/
- Python, Django, HTML, CSS
- Getting Started
- Create a Django project with one app, 'library'
- Add generic custom 404 for page not found
- Add views and templates for a home and books show page (
/
and/books/<id>
) - The home page will list all the book titles
- We have decided to display our books list on a separate page (which requires authentication)
- The show page will list the chosen book title and author
- Show a custom 404 if book is not found
- Use dummy data for now:
- We skipped this step!
[
{ 'id': 1, 'title': 'Life, the Universe and Everything', 'author': 'Douglas Adams'},
{ 'id': 2, 'title': 'The Meaning of Liff' 'author': 'Douglas Adams'},
{ 'id': 3, 'title': 'The No. 1 Ladies\' Detective Agency' 'author': 'Alexander McCall Smith'}
]
-
Add database and models
- Setup a 'library' database (your choice of db)
- Create models for Book and Author
- Authors have a name
- Books have a title and a Foreign Key for author
- Make and run migrations
- Add your models to your admin dashboard and create some records in the GUI
- Update your views to take data from the database instead of the dummy data list
-
Add User authentication
- Create register, login and logout routes
- Use the UserCreationForm to complete your templates
- Register some new users via your registration form
- Make the book show route only available to logged in users
- Add a new field of 'borrower' to the Book model as a Foreign Key to User
- In your admin dashboard, assign some books a borrower
- Update your book show template to show if it is available to loan or not
-
Add UI forms
- Create a new book form accessible at
books/new
- On success, the form redirects to the new book's show page
- Create a button on the book show page
- If the book is available, on clicking the button, the current User becomes the borrower of that book
- If the book is on loan by another User, current User sees disabled button
- If the book is on loan by the current User, on clicking the button, the book is returned
- Create a new book form accessible at
- Created a Django app which has a database and utilises user authentication
- Managed to write some tests and get them to pass
- Implementing the logic whereby a book can be returned if it is on loan by the current user. This was later solved by using an if else statement in the views file
- Initial difficulties with understanding the Django flow and getting the html templates to load
- No known bugs