Commit ba8bd205 authored by Ian Rafael T. Aragoza's avatar Ian Rafael T. Aragoza

Edited home and details pages, added add and edit functionalities

parent 499d5403
from django import forms
from .models import Author, Book
author_list = []
for author in Author.objects.all():
author_list.append(author)
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
\ No newline at end of file
......@@ -5,7 +5,6 @@
{% bootstrap_css %}
{% bootstrap_javascript %}
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% bootstrap_css %}{% endblock %}
</head>
<body>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Author{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<div class="container-fluid p-3 bg-dark text-white text-center">
<br>
<h1 class="display-4">Add a New Author to the Database</h1>
<br>
</div>
<div class="container my-5 text-center">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }} <br> {{ field }}<br><br>
{% endfor %}
<input type="submit" value="Add Author" class="btn btn-primary">
</form>
</div>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Book{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<div class="container-fluid p-3 bg-dark text-white text-center">
<br>
<h1 class="display-4">Add a New Book to the Database</h1>
<br>
</div>
<div class="container my-5 text-center">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }} <br> {{ field }}<br><br>
{% endfor %}
<input type="submit" value="Add Book" class="btn btn-primary">
</form>
</div>
{% endblock %}
......@@ -31,6 +31,8 @@
</div>
</div>
<p><i>{{ object.bio | linebreaks }}</i></p>
<br>
<a href="/authors/{{ object.pk }}/edit/" class="btn btn-primary">Edit Author</a>
</div>
<div class="container my-5 text-center">
<h3>Books by {{ object.first_name }} {{ object.last_name }} I Love:</h3>
......
......@@ -30,6 +30,9 @@
</div>
</div>
</div>
<br>
<a href="/books/{{ object.pk }}/edit" class="btn btn-primary">Edit Book</a>
<br><br>
</div>
<div class="container my-5 text-center">
<p><i>{{ object.blurb | linebreaks}}</i></p>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Author{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<div class="container-fluid p-3 bg-dark text-white text-center">
<br>
<h1 class="display-4">Edit Author's Details</h1>
<br>
</div>
<div class="container my-5 text-center">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }} <br> {{ field }}<br><br>
{% endfor %}
<input type="submit" value="Save Changes" class="btn btn-primary" href="/books/{{ object.pk }}/edit">
</form>
</div>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Book{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<div class="container-fluid p-3 bg-dark text-white text-center">
<br>
<h1 class="display-4">Edit Book's Details</h1>
<br>
</div>
<div class="container my-5 text-center">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }} <br> {{ field }}<br><br>
{% endfor %}
<input type="submit" value="Save Changes" class="btn btn-primary">
</form>
</div>
{% endblock %}
......@@ -24,7 +24,7 @@
No matter the case, I've had adventures with each of these books, and even though I don't remember them well, they've still left a mark on me.</p>
<br><br>
<div class="container">
<div class="row">
<div class="row p-3">
<div class="col-6">
<a href="/books"><button type="button" class="btn btn-primary">Books</button></a>
</div>
......@@ -32,6 +32,14 @@
<a href="/authors"><button type="button" class="btn btn-primary">Authors</button></a>
</div>
</div>
<div class="row p-3">
<div class="col-6">
<a href="/books/add"><button type="button" class="btn btn-primary">Add Book</button></a>
</div>
<div class="col-6">
<a href="/authors/add"><button type="button" class="btn btn-primary">Add Author</button></a>
</div>
</div>
</div>
</div>
{% endblock %}
\ No newline at end of file
# <appname>/urls.py
from django.urls import path
from .views import home, AuthorsView, AuthorDetailsView, BooksView, BookDetailsView
from django.views.generic import RedirectView
from .views import home, AuthorsView, AuthorDetailsView, BooksView, BookDetailsView, BookCreateView, BookUpdateView, AuthorCreateView, AuthorUpdateView
urlpatterns = [
path('home/', home, name='home'),
path('authors/', AuthorsView.as_view(), name='author-list'),
path('authors/add/', AuthorCreateView.as_view(), name='add-author'),
path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='author-detail'),
path('authors/<int:pk>/edit/', AuthorUpdateView.as_view(), name='edit-author'),
path('books/', BooksView.as_view(), name='books'),
path('books/add/', BookCreateView.as_view(), name='add-book'),
path('books/<int:pk>/details/', BookDetailsView.as_view(), name='books-detail'),
path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='edit-book'),
]
......
from django.shortcuts import render
from django.http.response import HttpResponseRedirect
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from .models import Author, Book
from .forms import AuthorForm, BookForm
# Create your views here.
def home(request):
......@@ -21,6 +22,30 @@ class BookDetailsView(DetailView):
template_name = "bookshelf/book_detais.html"
class BookCreateView(CreateView):
model = Book
fields = '__all__'
template_name = "bookshelf/add-book.html"
def post(self, request, *args, **kwargs):
form = BookForm(request.POST)
if form.is_valid():
new_book = form.save()
redirect_link = "../" + new_book.get_absolute_url() + "/details/"
return HttpResponseRedirect(redirect_link)
else:
return render(request, self.template_name, {'form': form})
class BookUpdateView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
success_url = "../details/"
class AuthorsView(ListView):
model = Author
template_name = "bookshelf/authors.html"
......@@ -29,3 +54,27 @@ class AuthorsView(ListView):
class AuthorDetailsView(DetailView):
model = Author
template_name = "bookshelf/author_details.html"
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = "bookshelf/add-author.html"
def post(self, request, *args, **kwargs):
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
redirect_link = "../" + new_author.get_absolute_url() + "/details/"
return HttpResponseRedirect(redirect_link)
else:
return render(request, self.template_name, {'form': form})
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
success_url = "../details/"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment