add book has been created

parent 9067f927
......@@ -4,4 +4,13 @@ from .models import Author, Books
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file
fields = ['first_name', 'last_name',
'age', 'nationality',
'bio']
class BookForm(forms.ModelForm):
class Meta:
model = Books
fields = ['title', 'author', 'publisher',
'year_published', 'ISBN',
'blurb']
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Book {% endblock %}
{% block content %}
<h1>Add New Book</h1>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
......@@ -12,5 +12,5 @@
</p>
<br>
<p><a href="http://127.0.0.1:8000/bookshelf/books">Books</a> <a href="http://127.0.0.1:8000/bookshelf/authors">Authors</a> </p>
<p><a href="http://127.0.0.1:8000/bookshelf/authors/add/">Add Author</a></p>
<p><a href="http://127.0.0.1:8000/bookshelf/books/add/">Add Book</a> <a href="http://127.0.0.1:8000/bookshelf/authors/add/">Add Author</a> </p>
{% endblock %}
\ No newline at end of file
......@@ -2,6 +2,7 @@ from django.urls import path
from .views import (home_view,
BooksListView, BooksDetailView,
BooksCreateView, BooksUpdateView,
AuthorsListView, AuthorsDetailView,
AuthorsCreateView, AuthorsUpdateView
)
......@@ -9,11 +10,13 @@ from .views import (home_view,
urlpatterns = [
path('home', home_view, name='index'),
path('books', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('books/<pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('books/add/', BooksCreateView.as_view(), name='books-add'),
path('books/<pk>/edit/', BooksUpdateView.as_view(), name='books-edit'),
path('authors', AuthorsListView.as_view(), name='authors-list'),
path('authors/<pk>/details/', AuthorsDetailView.as_view(), name='authors-detail'),
path('authors/add/', AuthorsCreateView.as_view(), name='authors-add'),
path('authors/<pk>/edit/', AuthorsUpdateView, name='authors-edit')
path('authors/<pk>/edit/', AuthorsUpdateView.as_view(), name='authors-edit')
]
app_name = "bookshelf"
\ No newline at end of file
......@@ -7,7 +7,7 @@ from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import Author, Books
from .forms import AuthorForm
from .forms import AuthorForm, BookForm
# Create your views here.
def home_view(request):
......@@ -31,7 +31,7 @@ class AuthorsCreateView(CreateView):
class AuthorsUpdateView(UpdateView):
model = Author
field = '__all__'
fields = '__all__'
template_name = 'bookshelf/authors_details.html'
class BooksListView(ListView):
......@@ -45,6 +45,16 @@ class BooksDetailView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
class BooksCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class BooksUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/book_details.html'
def author_view(request):
if request.method == 'POST':
form = AuthorForm(request.POST)
......@@ -54,3 +64,13 @@ def author_view(request):
else:
form = AuthorForm()
return render(request, 'add-author.html', {'form': form})
def book_view(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
new_author = form.save()
return redirect(AuthorsDetailView, pk=new_author.pk)
else:
form = AuthorForm()
return render(request, 'add-book.html', {'form': form})
\ 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