Commit 02285178 authored by justin's avatar justin

misc: updated forms.py to use ModelForm API, added url routing to /books/add

parent ffbf4449
from django import forms
from .models import Author, Books
class BookForm(forms.Form):
title = forms.CharField(label="Title", max_length=255)
publisher = forms.CharField(label="Publisher", max_length=255)
year_published = forms.IntegerField(label="Year Published")
isbn = forms.IntegerField(label="ISBN")
blurb = forms.CharField(label="Blurb", widget=forms.Textarea)
class BookForm(forms.ModelForm):
# title = forms.CharField(label="Title", max_length=255)
# publisher = forms.CharField(label="Publisher", max_length=255)
# year_published = forms.IntegerField(label="Year Published")
# isbn = forms.IntegerField(label="ISBN")
# blurb = forms.CharField(label="Blurb", widget=forms.Textarea)
class Meta:
model = Books
fields = [
"title",
"author",
"publisher",
"year_published",
"isbn",
"blurb",
]
{% 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="/books/add" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Submit" />
</form>
{% endblock %}
from django.urls import path
from .views import (
index,
add_book,
BookListView,
BookDetailView,
AuthorListView,
......@@ -11,6 +12,7 @@ urlpatterns = [
path("home/", index, name="home"),
path("books/", BookListView.as_view(), name="book-list"),
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"),
]
......@@ -3,6 +3,7 @@ from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Author, Books
from .forms import BookForm
def index(request):
......@@ -12,6 +13,11 @@ def index(request):
)
def add_book(request):
form = BookForm()
return render(request, "bookshelf/add-book.html", {"form": form})
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