Commit b5b638cc authored by karin-kurusu's avatar karin-kurusu

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

parent 0ed1c915
from django import forms from django import forms
from .models import Books from .models import Books, Author
class BooksForm(forms.ModelForm): class BooksForm(forms.ModelForm):
class Meta: class Meta:
model = Books model = Books
fields = ['title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb'] fields = ['title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb']
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Author{% endblock %}
{% block content %}
<form action='' method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Author">
</form>
{% 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, BooksCreate from .views import home, BooksList, BooksDetails, AuthorList, AuthorDetails, BooksCreate, AuthorCreate
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
...@@ -8,6 +8,7 @@ urlpatterns = [ ...@@ -8,6 +8,7 @@ urlpatterns = [
path('books/add', BooksCreate.as_view(), name='add-book'), 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'),
path('authors/add', AuthorCreate.as_view(), name='add-author'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
...@@ -28,6 +28,16 @@ class BooksUpdate(UpdateView): ...@@ -28,6 +28,16 @@ class BooksUpdate(UpdateView):
model = Books model = Books
fields = '__all__' fields = '__all__'
template_name = 'bookshelf/add-book.html' template_name = 'bookshelf/add-book.html'
class AuthorCreate(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class AuthorUpdate(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.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