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 django import forms
from .models import Author, Book 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 BookForm(forms.ModelForm):
class Meta: class Meta:
model = Book model = Book
...@@ -23,4 +9,4 @@ class BookForm(forms.ModelForm): ...@@ -23,4 +9,4 @@ class BookForm(forms.ModelForm):
class AuthorForm(forms.ModelForm): class AuthorForm(forms.ModelForm):
class Meta: class Meta:
model = Author model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio '] fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file \ No newline at end of file
...@@ -16,6 +16,9 @@ class Author(models.Model): ...@@ -16,6 +16,9 @@ class Author(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:author-detail', kwargs={'pk' : self.pk}) return reverse('bookshelf:author-detail', kwargs={'pk' : self.pk})
class Meta:
unique_together = ['first_name', 'last_name', 'age', 'nationality', 'bio']
class Book(models.Model): class Book(models.Model):
title = models.CharField(default="", max_length=100) title = models.CharField(default="", max_length=100)
...@@ -32,3 +35,6 @@ class Book(models.Model): ...@@ -32,3 +35,6 @@ class Book(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk' : self.pk}) return reverse('bookshelf:books-detail', kwargs={'pk' : self.pk})
class Meta:
unique_together = ['title', 'author', 'publisher', 'ISBN', 'blurb']
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% block title %} Add new book {% endblock %} {% block title %} Add New Author {% endblock %}
{% block forms %}{% endblock %}
{% block content %} {% block content %}
{{ form.non_field_errors }} <form method="post">
{% 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">
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form.as_p }}
<input type="submit" value="Submit"> <input type="submit" value="Add Author">
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends "base.html" %} {% extends "base.html" %}
{% load static %} {% load static %}
{% block title %} Add new book {% endblock %} {% block title %} Add New Book {% endblock %}
{% block forms %}{% endblock %}
{% block content %} {% block content %}
{{ form.non_field_errors }} <form method="post">
{% 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">
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form.as_p }}
<input type="submit" value="Submit"> <input type="submit" value="Add Book">
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
{% 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
{% 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 @@ ...@@ -3,21 +3,11 @@
{% block title %} Edit book {% endblock %} {% block title %} Edit book {% endblock %}
{% block forms %} {% block forms %}{% endblock %}
{{ form.non_field_errors }} {% block content %}
{% for field in form %} <form method="post">
{% 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 %} {% csrf_token %}
{{ form }} {{ form.as_p }}
<input type="submit" value="Submit"> <input type="submit" value="Save Changes">
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
down-to-earth and realistic themes. <br> <br> down-to-earth and realistic themes. <br> <br>
tl;dr: I em now weeb <br> <br> tl;dr: I em now weeb <br> <br>
<a href="/books/ ">Books</a> | <a href="/authors/">Authors</a><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> <h4>dsg ftw</h4>
{% endblock %} {% endblock %}
...@@ -4,18 +4,21 @@ from .views import ( ...@@ -4,18 +4,21 @@ from .views import (
AuthorDetailView, BookDetailView, AuthorDetailView, BookDetailView,
home, BookListView, AuthorListView, home, BookListView, AuthorListView,
BookCreateView, AuthorCreateView, BookCreateView, AuthorCreateView,
BookUpdateView, AuthorUpdateView BookUpdateView, AuthorUpdateView,
#author_edit, book_edit
) )
urlpatterns = [ urlpatterns = [
path('home/', home, name='home'), path('home/', home, name='home'),
path("authors/", AuthorListView.as_view(), name="author-list"), 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("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("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("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" 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.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Book from .models import Author, Book
from .forms import AuthorForm, BookForm
# Create your views here. # Create your views here.
...@@ -13,11 +14,6 @@ def home(request): ...@@ -13,11 +14,6 @@ def home(request):
class BookListView(ListView): class BookListView(ListView):
model = Book model = Book
class BookUpdateView(UpdateView):
model = Book
fields = '__all__'
template_name = 'bookshelf/edit-book.html'
class BookDetailView(DetailView): class BookDetailView(DetailView):
model = Book model = Book
...@@ -27,16 +23,34 @@ class BookCreateView(CreateView): ...@@ -27,16 +23,34 @@ class BookCreateView(CreateView):
model = Book model = Book
fields = '__all__' fields = '__all__'
template_name = 'bookshelf/add-book.html' template_name = 'bookshelf/add-book.html'
# template to be used by default is called <model>_form.html success_url = '/books'
class AuthorListView(ListView):
model = Author
class AuthorUpdateView(UpdateView): def post(self, request):
model = Author 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__' 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): class AuthorDetailView(DetailView):
model = Author model = Author
template_name = 'bookshelf/author_detail.html' template_name = 'bookshelf/author_detail.html'
...@@ -50,6 +64,56 @@ class AuthorCreateView(CreateView): ...@@ -50,6 +64,56 @@ class AuthorCreateView(CreateView):
model = Author model = Author
fields = '__all__' fields = '__all__'
template_name = 'bookshelf/add-author.html' 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 @@ ...@@ -53,7 +53,17 @@
<p>{% block content %}{% endblock %}</p> <p>{% block content %}{% endblock %}</p>
</div> </div>
<div class = "forms"> <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> </div>
</body> </body>
</html> </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