Commit 892f63d3 authored by justin's avatar justin

feat: added new author functionality with url routing complete

parent b3e1c631
......@@ -18,3 +18,9 @@ class BookForm(forms.ModelForm):
"isbn",
"blurb",
]
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = "__all__"
{% extends 'base.html' %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{field.label}} has errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form action="/authors/add" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Submit" />
</form>
{% endblock %}
......@@ -2,6 +2,7 @@ from django.urls import path
from .views import (
index,
add_book,
add_author,
BookListView,
BookDetailView,
AuthorListView,
......@@ -14,5 +15,6 @@ urlpatterns = [
path("books/<int:pk>/details", BookDetailView.as_view(), name="book-detail"),
path("books/add", add_book, name="add-book"),
path("authors/", AuthorListView.as_view(), name="author-list"),
path("author/<int:pk>/details", AuthorDetailView.as_view(), name="author-detail"),
path("authors/add", add_author, name="add-author"),
path("authors/<int:pk>/details", AuthorDetailView.as_view(), name="author-detail"),
]
......@@ -3,7 +3,7 @@ from django.urls import reverse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Author, Books
from .forms import BookForm
from .forms import AuthorForm, BookForm
def index(request):
......@@ -26,6 +26,19 @@ def add_book(request):
return render(request, "bookshelf/add-book.html", context)
def add_author(request):
if request.method == 'POST':
form = AuthorForm(request.POST)
if form.is_valid():
author = form.save()
pk = author.pk
return redirect(reverse("author-detail", args=[pk]))
else:
form = AuthorForm()
context = {"form": form}
return render(request, "bookshelf/add-author.html", context)
class BookListView(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