Commit 295a0ad7 authored by Deokhyun Lee's avatar Deokhyun Lee

added addbook html and view is attached.

parent ce62230b
from django import forms
from .models import Author
class AddBookForm(forms.Form):
title = forms.CharField(max_length = 100)
author = forms.ModelChoiceField(queryset=Author.objects.all())
publisher = forms.CharField(max_length = 100)
year_published = forms.DateTimeField()
ISBN = forms.CharField(max_length = 13)
blurb = forms.CharField(max_length = 250)
{% extends 'base.html'%}
{% block title %}
Add New Book
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Book</button>
</form>
{% endblock %}
\ No newline at end of file
......@@ -15,5 +15,7 @@
{% else %}
<p>No Available Books.</p>
{% endif %}
<a href="/home">Home</a>&nbsp&nbsp&nbsp&nbsp&nbsp<a href="/authors">Authors</a>
{% endblock %}
\ No newline at end of file
......@@ -11,5 +11,8 @@
Therefore, Frank Herbert's
Dune is one of the best
American authors for me!</p></p>
<a href="/books">Books</a>&nbsp&nbsp&nbsp&nbsp&nbsp<a href="/authors">Authors</a>
<ul>
<li><a href="/books">Books</a>&nbsp&nbsp&nbsp&nbsp&nbsp<a href="/authors">Authors</a></li>
<li><a href="/books/add">Add Book</a>&nbsp&nbsp&nbsp&nbsp&nbsp<a href="/authors/add">Add Author</a></li>
</ul>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import HomeView, AuthorListView, AuthorDetailView, BookListView, BookDetailView
from .views import HomeView, AuthorListView, AuthorDetailView, BookListView, BookDetailView, BookAddListView
urlpatterns = [
# for Home (landing page)
......@@ -10,4 +10,5 @@ urlpatterns = [
# for Books page
path('books/', BookListView.as_view(), name='books'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('books/add',BookAddListView.as_view(), name="add-book" )
]
\ No newline at end of file
......@@ -2,6 +2,7 @@ from django.views import View
from django.views.generic import ListView, DetailView
from django.shortcuts import render
from .models import Author, Books
from .forms import AddBookForm
# View for Home (landing page)
class HomeView(View):
......@@ -38,4 +39,21 @@ class BookListView(ListView):
class BookDetailView(DetailView):
model = Books
template_name = 'books/book_details.html'
context_object_name = 'book'
\ No newline at end of file
context_object_name = 'book'
class BookAddListView(ListView):
template_name = 'add-book.html'
form_class = AddBookForm
def form_valid(self, form):
# Create a new book object with the form data
new_book = Books.objects.create(
title=form.cleaned_data['title'],
author=form.cleaned_data['author'],
publisher=form.cleaned_data['publisher'],
year_published=form.cleaned_data['year_published'],
ISBN=form.cleaned_data['ISBN'],
blurb=form.cleaned_data['blurb']
)
# Redirect the user to the list of books
return super().form_valid(form)
.row {
grid-row: 1 / span 2;
}
\ No newline at end of file
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