Commit 35dca3f6 authored by EJ Mejilla's avatar EJ Mejilla

Added form pages for author and book creation.

Added hyperlinks to the form pages
parent 1e815aff
from django import forms
from .models import Books, Author
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name',
'last_name',
'age',
'nationality',
'bio'
]
class BooksForm(forms.ModelForm):
class Meta:
model = Books
fields = ['title',
'author',
'publisher',
'year_published',
'ISBN',
'blurb'
]
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Author {% endblock %}
{% block content %}
<div class="form">
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
</ul>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Book {% endblock %}
{% block content %}
<div class="form">
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
</div>
<ul class="footer">
<li><a href="{% url 'bookshelf:home' %}">Home</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
</ul>
{% endblock %}
......@@ -11,5 +11,8 @@
<li><a href="{% url 'bookshelf:books' %}">Books</a></li>
<li><a href="{% url 'bookshelf:authors' %}">Authors</a></li>
</ul>
<ul class="footer">
<li><a href="{% url 'bookshelf:book-create' %}">Add Books</a></li>
<li><a href="{% url 'bookshelf:author-create' %}">Add Authors</a></li>
</ul>
{% endblock %}
# bookshelf/urls.py
from django.urls import path
from .views import home_view, BookDetailView, BookListView, AuthorDetailView, AuthorListView
from .views import (
home_view, BookDetailView, BookListView, AuthorDetailView,
AuthorListView, AuthorCreateView, AuthorUpdateView, BooksCreateView,
BooksUpdateView
)
urlpatterns = [
path('', home_view, name='home'),
path('books/', BookListView.as_view(template_name='bookshelf/books.html'), name='books'),
path('books/<int:pk>/details/', BookDetailView.as_view(template_name="bookshelf/book_details.html"), name='book-details'),
path('authors/', AuthorListView.as_view(template_name='bookshelf/authors.html'), name='authors'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(template_name='bookshelf/author_details.html'), name='author-details'),
path('books/', BookListView.as_view(), name='books'),
path('books/<int:pk>/details/', BookDetailView.as_view(), name='book-details'),
path('books/add', BooksCreateView.as_view(), name='book-create'),
path('books/<int:pk>', BooksUpdateView.as_view(), name='book-update'),
path('authors/', AuthorListView.as_view(), name='authors'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='author-details'),
path('authors/add', AuthorCreateView.as_view(), name='author-create'),
path('authors/<int:pk>', BooksUpdateView.as_view(), name='author-update'),
]
app_name = "bookshelf"
......@@ -3,8 +3,10 @@ from django.shortcuts import render
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books
from .forms import AuthorForm, BooksForm
def home_view(request):
return render(request, 'bookshelf/home.html')
......@@ -12,15 +14,42 @@ def home_view(request):
class BookDetailView(DetailView):
model = Books
template_name="bookshelf/book_details.html"
class BookListView(ListView):
model = Books
template_name='bookshelf/books.html'
class AuthorDetailView(DetailView):
model = Author
template_name='bookshelf/author_details.html'
class AuthorListView(ListView):
model = Author
template_name='bookshelf/authors.html'
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = 'author_details.html'
class BooksCreateView(CreateView):
model = Books
fields = '__all__'
class BooksUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'book_details.html'
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