added new url paths and new author view classes

parent 36255369
Neal Luigi D. Rodriguez
204374
CSCI 40-F
Lab 03: My Favorite Books and Submission
Lab 04: My Favorite Books and Submission
Date of Submission: March 30, 2023
Date of Submission: April 25, 2023
This lab was truthfully completed by me.
I am the sole contributor and author of
......@@ -24,4 +24,4 @@ wikipedia and the penguin publishing website
<sgd>
Neal Luigi D. Rodriguez
March 30, 2023
\ No newline at end of file
April 25, 2023
\ No newline at end of file
from django import forms
from .models import Author, Books
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['first_name', 'last_name', 'age', 'nationality', 'bio']
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} Add New Author {% endblock %}
{% block content %}
<form action="" method="post">
{% csrf token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endblock %}
\ No newline at end of file
......@@ -2,15 +2,18 @@ from django.urls import path
from .views import (home_view,
BooksListView, BooksDetailView,
AuthorsListView, AuthorsDetailView
AuthorsListView, AuthorsDetailView,
AuthorsCreateView, AuthorsUpdateView
)
urlpatterns = [
path('home', home_view, name='index'),
path('books', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>', BooksDetailView.as_view(), name='books-detail'),
path('books/<int:pk>/details/', BooksDetailView.as_view(), name='books-detail'),
path('authors', AuthorsListView.as_view(), name='authors-list'),
path('authors/<int:pk>', AuthorsDetailView.as_view(), name='authors-detail')
path('authors/<pk>/details/', AuthorsDetailView.as_view(), name='authors-detail'),
path('authors/add/', AuthorsCreateView, name='authors-add'),
path('authors/<pk>/edit/', AuthorsUpdateView, name='authors-edit')
]
app_name = "bookshelf"
\ No newline at end of file
......@@ -2,8 +2,12 @@ from django.shortcuts import render
from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import Author, Books
from .forms import AuthorForm
# Create your views here.
def home_view(request):
......@@ -20,6 +24,16 @@ class AuthorsDetailView(DetailView):
model = Author
template_name = 'bookshelf/authors_details.html'
class AuthorsCreateView(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-author.html'
class AuthorsUpdateView(UpdateView):
model = Author
field = '__all__'
template_name = 'bookshelf/authors_details.html'
class BooksListView(ListView):
def get(self, request):
books = Books.objects.all()
......@@ -30,4 +44,13 @@ class BooksListView(ListView):
class BooksDetailView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
\ No newline at end of file
def author_view(request):
if request.method == 'POST':
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
return redirect(AuthorsDetailView, pk=new_author.pk)
else:
form = AuthorForm()
return render(request, 'add-author.html', {'form': form})
\ 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