Commit 3b240356 authored by John Riz Daniel Ramos's avatar John Riz Daniel Ramos

Edit book, Add Author, Edit Author

+ Added the classes and functionality of each
parent f25f109b
......@@ -6,7 +6,7 @@ class BooksForm(forms.ModelForm):
model = Books
fields = '__all__'
class BooksForm(forms.ModelForm):
class AuthorsForm(forms.ModelForm):
class Meta:
model = Author
fields = '__all__'
\ No newline at end of file
from django.urls import path
from .views import home_view, BooksListView, BooksDetailView, AuthorsView, AuthorsDetailView, BooksCreateView, BooksUpdateView
from .views import home_view, BooksListView, BooksDetailView, AuthorsView, AuthorsDetailView, BooksCreateView, BooksUpdateView, AuthorsCreateView, AuthorsUpdateView
urlpatterns = [
......@@ -7,9 +7,11 @@ urlpatterns = [
path('books/', BooksListView.as_view(), name='books-list'),
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('books/<int:pk>/edit/', BooksUpdateView.as_view(), name='books-edit'),
path('authors/', AuthorsView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorsDetailView.as_view(), name='authors-detail'),
path('authors/add/', AuthorsCreateView.as_view(), name='authors-add'),
path('authors/<int:pk>/edit/', AuthorsUpdateView.as_view(), name='authors-edit'),
]
app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render, redirect
from django.shortcuts import render
from django.http.response import HttpResponseRedirect
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, Books
from .forms import BooksForm
from .forms import BooksForm, AuthorsForm
def home_view(request):
......@@ -43,6 +43,9 @@ class BooksUpdateView(UpdateView):
fields = '__all__'
template_name = "bookshelf/edit-book.html"
success_url = "../details/"
class AuthorsView(ListView):
model = Author
template_name = "bookshelf/authors.html"
......@@ -53,6 +56,30 @@ class AuthorsDetailView(DetailView):
template_name = "bookshelf/author_details.html"
class AuthorsCreateView(CreateView):
model = Author
fields = '__all__'
template_name = "bookshelf/add-author.html"
def post(self, request, *args, **kwargs):
form = AuthorsForm(request.POST)
if form.is_valid():
new_author = form.save()
detail_link = "../" + new_author.get_absolute_url() + "/details"
return HttpResponseRedirect(detail_link)
else:
return render(request, self.template_name, {'form': form})
class AuthorsUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = "bookshelf/edit-author.html"
success_url = "../details/"
# def books_view(request):
# if request.method == 'POST':
# form = BooksForm(request.POST)
......
{% 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 method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Author">
</form>
{% endblock %}
\ No newline at end of file
......@@ -16,7 +16,7 @@
{% endfor %}
<form method="post">
{% csrf_token %}
{{ form }}
{{ form.as_p }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
\ No newline at end of file
......@@ -16,6 +16,12 @@
</li>
{% endfor %}
<br>
<a href="/authors/{{ object.get_absolute_url }}/edit">
<input type="submit" value="Edit Author">
</a>
<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.as_p }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
......@@ -16,7 +16,7 @@
{% endfor %}
<form method="post">
{% csrf_token %}
{{ form }}
{{ form.as_p }}
<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