Commit 90517f7d authored by Julia Anishka's avatar Julia Anishka

added new author page

parent d6cf867f
from django.forms import ModelForm
from .models import Books
from .models import Books, Author
class BooksForm(ModelForm):
class Meta:
model = Books
fields = '__all__'
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = '__all__'
\ No newline at end of file
{% extends 'base.html' %}
{% block title %} Add New Author {% endblock %}
{% block body %}
<form method='POST'>
{% csrf_token %}
{{ form }}
<input type='Submit' value='Add Author'>
</form>
{% endblock %}
\ No newline at end of file
......@@ -2,7 +2,7 @@ from django.urls import path
from . import views
from .views import (BooksListView, BooksDetailView, BooksCreateView, AuthorsListView,
AuthorsDetailView, )
AuthorsDetailView, AuthorsCreateView, )
urlpatterns = [
path('home/', views.homepage_view, name='home'),
......@@ -10,7 +10,8 @@ urlpatterns = [
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='book_details'),
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')
path('authors/<int:pk>/details/', AuthorsDetailView.as_view(), name='author_details'),
path('home/authors/add/', AuthorsCreateView.as_view(), name='add-author'),
]
app_name = 'bookshelf'
\ No newline at end of file
......@@ -6,7 +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
from .forms import BooksForm, AuthorForm
def homepage_view(request):
authors = Author.objects.all()
......@@ -35,6 +35,24 @@ def add_book_view(request):
form = BooksForm()
return render(request, 'bookshelf/add-book.html', {'form': form})
def add_author_view(request):
if request.method == 'POST':
form = AuthorForm(request.POST)
if form.is_valid():
return HttpResponse('Title: {}'.format(
first_name = form.cleaned_data['first_name'],
last_name = form.cleaned_data['last_name'],
age = form.cleaned_data['age'],
nationality = form.cleaned_data['nationality'],
bio = form.cleaned_data['bio']
)
)
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'
......@@ -56,3 +74,8 @@ class AuthorsDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
class AuthorsCreateView(CreateView):
model = Author
template_name = 'bookshelf/add-author.html'
fields = '__all__'
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