Commit f25f109b authored by John Riz Daniel Ramos's avatar John Riz Daniel Ramos

Add Book Function

+ Finished Add Book Functionality
parent 7fa2cc8c
from django import forms
from .models import Books, Author
class BooksForm(forms.ModelForm):
class Meta:
model = Books
fields = '__all__'
class BooksForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
\ No newline at end of file
...@@ -5,8 +5,9 @@ from .views import home_view, BooksListView, BooksDetailView, AuthorsView, Autho ...@@ -5,8 +5,9 @@ from .views import home_view, BooksListView, BooksDetailView, AuthorsView, Autho
urlpatterns = [ urlpatterns = [
path('home/', home_view, name='home_view'), path('home/', home_view, name='home_view'),
path('books/', BooksListView.as_view(), name='books-list'), path('books/', BooksListView.as_view(), name='books-list'),
path('books/add', BooksCreateView.as_view(), name='books-create'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'), path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('books/add/', BooksCreateView.as_view(), name='books-add'),
path('books/<int:pk>/edit/', BooksUpdateView.as_view(), name='books-add'),
path('authors/', AuthorsView.as_view(), name='authors-list'), path('authors/', AuthorsView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorsDetailView.as_view(), name='authors-detail'), path('authors/<int:pk>/details/', AuthorsDetailView.as_view(), name='authors-detail'),
] ]
......
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http.response import HttpResponseRedirect
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, Books from .models import Author, Books
from .forms import BooksForm
def home_view(request): def home_view(request):
...@@ -17,7 +19,7 @@ class BooksListView(ListView): ...@@ -17,7 +19,7 @@ class BooksListView(ListView):
class BooksDetailView(DetailView): class BooksDetailView(DetailView):
model = Books model = Books
template_name = "bookshelf/book_detais.html" template_name = "bookshelf/book_details.html"
class BooksCreateView(CreateView): class BooksCreateView(CreateView):
...@@ -25,12 +27,21 @@ class BooksCreateView(CreateView): ...@@ -25,12 +27,21 @@ class BooksCreateView(CreateView):
fields = '__all__' fields = '__all__'
template_name = "bookshelf/add-book.html" template_name = "bookshelf/add-book.html"
def post(self, request, *args, **kwargs):
form = BooksForm(request.POST)
if form.is_valid():
new_book = form.save()
detail_link = "../" + new_book.get_absolute_url() + "/details"
return HttpResponseRedirect(detail_link)
else:
return render(request, self.template_name, {'form': form})
class BooksUpdateView(UpdateView): class BooksUpdateView(UpdateView):
model = Books model = Books
fields = '__all__' fields = '__all__'
template_name = "bookshelf/add-book.html" template_name = "bookshelf/edit-book.html"
class AuthorsView(ListView): class AuthorsView(ListView):
model = Author model = Author
...@@ -40,3 +51,14 @@ class AuthorsView(ListView): ...@@ -40,3 +51,14 @@ class AuthorsView(ListView):
class AuthorsDetailView(DetailView): class AuthorsDetailView(DetailView):
model = Author model = Author
template_name = "bookshelf/author_details.html" template_name = "bookshelf/author_details.html"
# def books_view(request):
# if request.method == 'POST':
# form = BooksForm(request.POST)
# if form.is_valid():
# new_subject = form.save()
# return redirect('book_details', pk=new_subject.pk)
# else:
# form = BooksForm()
# return render(request, 'book_details', {'form': form})
\ No newline at end of file
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
</ul> </ul>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<form action="/books" method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form }}
<input type="submit" value="Submit"> <input type="submit" value="Add Book">
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -12,6 +12,12 @@ ...@@ -12,6 +12,12 @@
<i>{{ object.blurb }}</i> <i>{{ object.blurb }}</i>
</p> </p>
<br>
<a href="/books/{{ object.get_absolute_url }}/edit">
<input type="submit" value="Edit Book">
</a>
<br> <br>
<br> <br>
<br> <br>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit{% 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 method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ 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