Commit fad54cde authored by Albert Gagalac's avatar Albert Gagalac

added page creation/editing logic for Author and Books with their respective views in views.py

parent f32f0d42
from django import forms
from .models import Author, Book
# class AuthorForm(forms.Form):
# first_name = forms.CharField(label="First Name", max_length=100)
# last_name = forms.CharField(label="Last Name", max_length=100)
# age = forms.IntegerField(label="Age",min_value=0)
# nationality = forms.CharField(label="Nationality", max_length=100)
# bio = forms.CharField(label="Bio", max_length = 700)
# class BookForm(forms.Form):
# title = forms.CharField(label="Title", max_length=100)
# author = forms.ModelChoiceField(queryset=Author.objects.all())
# publisher = forms.CharField(label="Publisher", max_length=100)
# ISBN = forms.IntegerField(label="ISBN")
# blurb = forms.CharField(label="Blurb", max_length = 700)
class BookForm(forms.ModelForm):
class Meta:
model = Book
......@@ -23,4 +9,4 @@ class BookForm(forms.ModelForm):
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio ']
\ No newline at end of file
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file
......@@ -16,6 +16,9 @@ class Author(models.Model):
def get_absolute_url(self):
return reverse('bookshelf:author-detail', kwargs={'pk' : self.pk})
class Meta:
unique_together = ['first_name', 'last_name', 'age', 'nationality', 'bio']
class Book(models.Model):
title = models.CharField(default="", max_length=100)
......@@ -32,3 +35,6 @@ class Book(models.Model):
def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk' : self.pk})
class Meta:
unique_together = ['title', 'author', 'publisher', 'ISBN', 'blurb']
{% extends "base.html" %}
{% load static %}
{% block title %} Add new book {% endblock %}
{% block title %} Add New Author {% endblock %}
{% block forms %}{% 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="/" method="post">
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
{{ form.as_p }}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block title %} Add new book {% endblock %}
{% block title %} Add New Book {% endblock %}
{% block forms %}{% 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 %}<br>
<form action="/" method="post">
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
{{ form.as_p }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
{% comment %} {% block forms %}
{{ 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="/index_card" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %} {% endcomment %}
\ No newline at end of file
{% endblock %}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block title %} Edit author {% endblock %}
{% block forms %}{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
......@@ -3,21 +3,11 @@
{% block title %} Edit book {% endblock %}
{% block forms %}
{{ 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="/index_card" method="post">
{% block forms %}{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
{{ form.as_p }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
......@@ -19,7 +19,7 @@
down-to-earth and realistic themes. <br> <br>
tl;dr: I em now weeb <br> <br>
<a href="/books/ ">Books</a> | <a href="/authors/">Authors</a><br>
<a href="/books/add/ ">Add Books</a> | <a href="/authors/add/">Add Authors</a><br>
<a href="/books/add">Add Books</a> | <a href="/authors/add">Add Authors</a><br>
<h4>dsg ftw</h4>
{% endblock %}
......@@ -4,18 +4,21 @@ from .views import (
AuthorDetailView, BookDetailView,
home, BookListView, AuthorListView,
BookCreateView, AuthorCreateView,
BookUpdateView, AuthorUpdateView
BookUpdateView, AuthorUpdateView,
#author_edit, book_edit
)
urlpatterns = [
path('home/', home, name='home'),
path("authors/", AuthorListView.as_view(), name="author-list"),
path("books/", BookListView.as_view(), name="books-list"),
path("books/add", BookCreateView.as_view(), name="books-add"),
path("authors/add", AuthorCreateView.as_view(), name="author-add"),
path("books/<int:pk>/edit", BookUpdateView.as_view(), name="books-edit"),
path("authors/<int:pk>/edit", AuthorUpdateView.as_view(), name="author-edit"),
path("books/<int:pk>/details", BookDetailView.as_view(), name="books-detail"),
path("authors/<int:pk>/details", AuthorDetailView.as_view(), name="author-detail"),
path("books/", BookListView.as_view(), name="books-list"),
path("books/add", BookCreateView.as_view(), name="books-add"),
path("books/<int:pk>/edit", BookUpdateView.as_view(), name="books-edit"),
path("books/<int:pk>/details", BookDetailView.as_view(), name="books-detail"),
]
app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Book
from .forms import AuthorForm, BookForm
# Create your views here.
......@@ -13,11 +14,6 @@ def home(request):
class BookListView(ListView):
model = Book
class BookUpdateView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
class BookDetailView(DetailView):
model = Book
......@@ -27,16 +23,34 @@ class BookCreateView(CreateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/add-book.html'
# template to be used by default is called <model>_form.html
class AuthorListView(ListView):
model = Author
success_url = '/books'
class AuthorUpdateView(UpdateView):
model = Author
def post(self, request):
form = BookForm(request.POST)
if form.is_valid():
book = form.save()
book_id = book.pk
return redirect('bookshelf:books-detail', pk=book_id)
else:
return render(request, self.template_name, {'form': form})
class BookUpdateView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
template_name = 'bookshelf/edit-book.html'
success_url = '/books'
def form_valid(self, form):
form.save()
book_id = self.kwargs['pk']
return redirect('bookshelf:books-detail', pk=book_id)
def form_invalid(self):
return redirect('bookshelf:books-list')
class AuthorListView(ListView):
model = Author
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_detail.html'
......@@ -50,6 +64,56 @@ class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
# template to be used by default is called <model>_form.html
def post(self, request):
form = AuthorForm(request.POST)
if form.is_valid():
author = form.save()
author_id = author.pk
return redirect('bookshelf:author-detail', pk=author_id)
else:
return render(request, self.template_name, {'form': form})
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/edit-author.html'
def form_valid(self, form):
form.save()
author_id = self.kwargs['pk']
return redirect('bookshelf:author-detail', pk=author_id)
def form_invalid(self):
return redirect('bookshelf:author-list')
# def author_edit(request, pk):
# author = get_object_or_404(Book, pk)
# if request.method == 'POST':
# form = AuthorForm(request.POST, instance=author)
# if form.is_valid():
# form.save()
# return redirect('author-detail', pk=author.pk)
# else:
# return render(request, 'author_detail.html', {'form': form})
# else:
# form = AuthorForm()
# return render(request, 'author_detail.html', {'form': form})
# def book_edit(request, pk):
# book = get_object_or_404(Book, pk)
# if request.method == 'POST':
# form = BookForm(request.POST, instance=book)
# if form.is_valid():
# form.save()
# return redirect('book-detail', pk=book.pk)
# else:
# return render(request, 'book_detail.html', {'form': form})
# else:
# form = BookForm()
# return render(request, 'book_detail.html', {'form': form})
......@@ -53,7 +53,17 @@
<p>{% block content %}{% endblock %}</p>
</div>
<div class = "forms">
<p>{% block forms %}{% endblock %}</p>
{% block forms %}
{{ form.non_field_errors }}
{% if field.errors %}
<p>{{ field.label }} has the following errors:</p>
<li>
{% for error in field.errors %}
{{ error }}
</li>
{% endfor %}
{% endif %}
{% endblock %}
</div>
</body>
</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