Implemented add-book and add-author views, html, urls, and forms w/out site redirect after submit

parent deba8acc
from django import forms
from .models import Book, Author
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb']
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<form action="http://localhost:8000/authors/add" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}
{{ field }}<br>
{% endfor %}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}My Favorite Books{% endblock %}
{% block content %}
<form action="http://localhost:8000/books/add" method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label }}
{{ field }}<br>
{% endfor %}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
...@@ -6,5 +6,7 @@ ...@@ -6,5 +6,7 @@
<h1>Welcome to Matt's Database of Favorite Books and Authors!<br></h1> <h1>Welcome to Matt's Database of Favorite Books and Authors!<br></h1>
<p>I like fantasy, sci-fi, and detective novels written by (perhaps) depressed and somewhat manic authors!<br></p> <p>I like fantasy, sci-fi, and detective novels written by (perhaps) depressed and somewhat manic authors!<br></p>
<a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/books/">Books</a>
<a href="http://localhost:8000/authors/">Authors</a> <a href="http://localhost:8000/authors/">Authors</a><br>
<a href="http://localhost:8000/books/add">Add Book</a>
<a href="http://localhost:8000/authors/add">Add Author</a>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import homepage, BooksView, BookDetailView, AuthorsView, AuthorDetailView from .views import homepage, BooksView, BookDetailView, AuthorsView, AuthorDetailView, AddNewBookView, AddNewAuthorView
urlpatterns = [ urlpatterns = [
path('home/', homepage, name='homepage'), path('home/', homepage, name='homepage'),
path('books/', BooksView.as_view(), name='book-list'), path('books/', BooksView.as_view(), name='book-list'),
path('books/add', AddNewBookView.as_view(), name='add-book'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'), path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('authors/', AuthorsView.as_view(), name='author-list'), path('authors/', AuthorsView.as_view(), name='author-list'),
path('authors/add', AddNewAuthorView.as_view(), name='add-author'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='author-detail'), path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='author-detail'),
] ]
......
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Book, Author from .models import Book, Author
from .forms import BookForm, AuthorForm
def homepage(request): def homepage(request):
return render(request, 'bookshelf/home.html') return render(request, 'bookshelf/home.html')
...@@ -26,3 +29,56 @@ class AuthorDetailView(DetailView): ...@@ -26,3 +29,56 @@ class AuthorDetailView(DetailView):
specificauthor = Author.objects.get(pk=pk) specificauthor = Author.objects.get(pk=pk)
authorbooklist = Book.objects.filter(author__exact=specificauthor) authorbooklist = Book.objects.filter(author__exact=specificauthor)
return render(request, 'bookshelf/author_details.html', {'author': specificauthor, 'booklist': authorbooklist}) return render(request, 'bookshelf/author_details.html', {'author': specificauthor, 'booklist': authorbooklist})
class AddNewBookView(DetailView):
model = Book
def get(self, request):
form = BookForm()
return render(request, 'bookshelf/add-book.html', {'form':form})
def post(self, request, *args, **kwargs):
form = BookForm(request.POST)
title = request.POST.get('title')
author = Author.objects.get(request.POST.get('author'))
publisher = request.POST.get('publisher')
year_published = request.POST.get('year_published')
ISBN = request.POST.get('ISBN')
blurb = request.POST.get('blurb')
s = Book(title=title, author=author, publisher=publisher, year_published=year_published, ISBN=ISBN, blurb=blurb)
s.save()
newbookpk = s.pk
newbook = Book.objects.get(pk=newbookpk)
if form.is_valid():
return render(request, 'bookshelf/book_details.html', {'book': newbook})
else:
return self.get(request, *args, **kwargs)
class AddNewAuthorView(DetailView):
model = Author
def get(self, request):
form = AuthorForm()
return render(request, 'bookshelf/add-author.html', {'form':form})
def post(self, request, *args, **kwargs):
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
age = request.POST.get('age')
nationality = request.POST.get('nationality')
bio = request.POST.get('bio')
s = Author(first_name=first_name, last_name=last_name, age=age, nationality=nationality, bio=bio)
s.save()
newauthorpk = s.pk
newauthor = Author.objects.get(pk=newauthorpk)
if form.is_valid():
return render(request, 'bookshelf/author_details.html', {'author': newauthor})
else:
return self.get(request, *args, **kwargs)
\ 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