Commit 0ed1c915 authored by karin-kurusu's avatar karin-kurusu

Created "Add New Book" page and implemented it via ModelForm

parent 8cb4706e
from django import forms
from .models import Books
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 Book{% endblock %}
{% block content %}
<form action='' method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
...@@ -22,4 +22,10 @@ ...@@ -22,4 +22,10 @@
<li class="navbarItem"><a href="/books">Books</a></li> <li class="navbarItem"><a href="/books">Books</a></li>
<li class="navbarItem"><a href="/authors">Authors</a></li> <li class="navbarItem"><a href="/authors">Authors</a></li>
</ul> </ul>
<ul class="navbarList">
<li class="navbarItem"><a href="/books/add">Add Book</a></li>
<li class="navbarItem"><a href="/authors/add">Add Author</a></li>
</ul>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import home, BooksList, BooksDetails, AuthorList, AuthorDetails from .views import home, BooksList, BooksDetails, AuthorList, AuthorDetails, BooksCreate
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path('books/', BooksList.as_view(), name='books'), path('books/', BooksList.as_view(), name='books'),
path('books/<int:pk>/details', BooksDetails.as_view(), name='book_detais'), path('books/<int:pk>/details', BooksDetails.as_view(), name='book_detais'),
path('books/add', BooksCreate.as_view(), name='add-book'),
path('authors/', AuthorList.as_view(), name="authors"), path('authors/', AuthorList.as_view(), name="authors"),
path('authors/<int:pk>/details', AuthorDetails.as_view(), name='author_details'), path('authors/<int:pk>/details', AuthorDetails.as_view(), name='author_details'),
] ]
......
...@@ -2,6 +2,7 @@ from django.shortcuts import render ...@@ -2,6 +2,7 @@ from django.shortcuts import render
from django.views import View from django.views import View
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books from .models import Author, Books
def home(request): def home(request):
...@@ -17,6 +18,16 @@ class BooksDetails(DetailView): ...@@ -17,6 +18,16 @@ class BooksDetails(DetailView):
book = Books.objects.get(pk=pk) book = Books.objects.get(pk=pk)
author = book.author author = book.author
return render(request, 'bookshelf/book_detais.html', {'author': author, 'book': book}) return render(request, 'bookshelf/book_detais.html', {'author': author, 'book': book})
class BooksCreate(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class BooksUpdate(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class AuthorList(ListView): class AuthorList(ListView):
def get(self, request): def get(self, request):
......
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