Commit 95bf4d51 authored by Alvin Joshua Andrada's avatar Alvin Joshua Andrada

implemented forms

parent 24cabb8d
# forms bookshelf
from django import forms
from .models import Author, Books
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'publisher','year_published','ISBN','blurb']
<!-- add-author html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Author{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form action="/authors" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
<!-- add-book html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Book{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form action="/books" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
<!-- edit-author html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Book{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form action="home/authors" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
<!-- edit-book html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Book{% endblock %}
{% block content %}
{{ form.non_field_errors }}
{% for field in form %}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
<form action="home/books" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
...@@ -9,5 +9,7 @@ in animes. I mostly read LitRPGs and Chinese genres. I often read in a ...@@ -9,5 +9,7 @@ in animes. I mostly read LitRPGs and Chinese genres. I often read in a
particular website wherein amateur writers upload this kind of work. Many particular website wherein amateur writers upload this kind of work. Many
of them are actually good.</p> of them are actually good.</p>
<a href="authors/">Authors</a> <a href="authors/">Authors</a>
<a href="books/">Books</a> <a href="books/">Books</a> <br>
<a href="books/add/">Add Book</a>
<a href="authors/add/">Add Author</a>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
from .views import BooksView, AuthorsView, BookDetailView, AuthorDetailView from .views import (BooksView, AuthorsView, BookDetailView, AuthorDetailView,
BookCreateView, AuthorCreateView, BookUpdateView, AuthorUpdateView
)
urlpatterns = [ urlpatterns = [
path('', views.home_view, name='home'), path('', views.home_view, name='home'),
...@@ -8,6 +10,10 @@ urlpatterns = [ ...@@ -8,6 +10,10 @@ urlpatterns = [
path('authors/', AuthorsView.as_view(), name='author-list'), path('authors/', AuthorsView.as_view(), name='author-list'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'), path('books/<int:pk>/details', BookDetailView.as_view(), name='book-detail'),
path('authors/<int:author_id>/details/', AuthorDetailView.as_view(), name='author-detail'), path('authors/<int:author_id>/details/', AuthorDetailView.as_view(), name='author-detail'),
path('books/add/', BookCreateView.as_view(), name='add-book'),
path('authors/add/', AuthorCreateView.as_view(), name='add-author'),
path('books/<int:pk>/edit', BookUpdateView.as_view(), name='edit-book'),
path('authors/<int:pk>/edit', AuthorUpdateView.as_view(), name='edit-author'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
...@@ -5,6 +5,8 @@ from django.views import View ...@@ -5,6 +5,8 @@ from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books from .models import Author, Books
def home_view(request): def home_view(request):
...@@ -26,4 +28,24 @@ class AuthorDetailView(View): ...@@ -26,4 +28,24 @@ class AuthorDetailView(View):
def get(self, request, author_id): def get(self, request, author_id):
author = Author.objects.get(pk=author_id) author = Author.objects.get(pk=author_id)
books_list= Books.objects.all().filter(author=author) books_list= Books.objects.all().filter(author=author)
return render(request, "bookshelf/author_details.html", {"author":author,"books_list":books_list}) return render(request, "bookshelf/author_details.html", {"author":author,"books_list":books_list})
\ No newline at end of file
class BookCreateView(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class BookUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class AuthorUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
\ No newline at end of file
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