Commit d6cf867f authored by Julia Anishka's avatar Julia Anishka

added new book template and created addbook function

parent 6db6428a
from django.forms import ModelForm
from .models import Books
class BooksForm(ModelForm):
class Meta:
model = Books
fields = '__all__'
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Add New Book {% endblock %}
{% block body %}
<form method='POST'>
{% csrf_token %}
{{ form }}
<input type='Submit' value='Add Book'>
</form>
{% endblock %}
\ No newline at end of file
......@@ -19,8 +19,8 @@
<div class="btns">
<a href="/books/" class="btn"> Books </a>
<a href="/authors/" class="btn"> Authors </a>
<a href="books/add" class="btn"> Authors </a>
<a href="authors/add" class="btn"> Authors </a>
<a href="books/add" class="btn"> Add Book </a>
<a href="authors/add" class="btn"> Add Author </a>
</div>
{% endblock %}
\ No newline at end of file
......@@ -8,7 +8,7 @@ urlpatterns = [
path('home/', views.homepage_view, name='home'),
path('books/', BooksListView.as_view(), name='books'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='book_details'),
path('books/add/', BooksCreateView.as_view(), name='add-book'),
path('home/books/add/', BooksCreateView.as_view(), name='add-book'),
path('authors/', AuthorsListView.as_view(), name='authors'),
path('authors/<int:pk>/details/', AuthorsDetailView.as_view(), name='author_details')
]
......
......@@ -6,6 +6,7 @@ from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView
from .models import Author, Books
from .forms import BooksForm
def homepage_view(request):
authors = Author.objects.all()
......@@ -16,6 +17,24 @@ def homepage_view(request):
}
return render(request, 'bookshelf/home.html', context)
def add_book_view(request):
if request.method == 'POST':
form = BooksForm(request.POST)
if form.is_valid():
return HttpResponse('Title: {}'.format(
title = form.cleaned_data['title'],
author = form.cleaned_data['author'],
year_published = form.cleaned_data['year_published'],
ISBN = form.cleaned_data['ISBN'],
blurb = form.cleaned_data['blurb']
)
)
else:
return render(request, 'bookshelf/add-book.html', {'form': form})
else:
form = BooksForm()
return render(request, 'bookshelf/add-book.html', {'form': form})
class BooksListView(ListView):
model = Books
template_name = 'bookshelf/books.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