Commit fb4cf22b authored by Joei Yucoco's avatar Joei Yucoco

Added working books/add/ url

parent 733e2684
<!DOCTYPE html>
{% extends 'base.html' %}
{% block title %}My Favorite Authors{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Book">
</form>
{% endblock %}
from django.urls import path from django.urls import path
from .views import HomeView, BooksView, BookDetailsView, AuthorsView, AuthorDetailsView, AuthorAddView from .views import (HomeView, BooksView, BookDetailsView,
AuthorsView, AuthorDetailsView, AuthorAddView, BookAddView)
urlpatterns = [ urlpatterns = [
path('home/', HomeView, name='home'), path('home/', HomeView, name='home'),
...@@ -8,6 +9,7 @@ urlpatterns = [ ...@@ -8,6 +9,7 @@ urlpatterns = [
path('authors/', AuthorsView.as_view(), name='authors-list'), path('authors/', AuthorsView.as_view(), name='authors-list'),
path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='authors-detail'), path('authors/<int:pk>/details/', AuthorDetailsView.as_view(), name='authors-detail'),
path('authors/add/', AuthorAddView, name='authors-add'), path('authors/add/', AuthorAddView, name='authors-add'),
path('books/add/', BookAddView, name='books-add'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
...@@ -34,15 +34,24 @@ class AuthorDetailsView(DetailView): ...@@ -34,15 +34,24 @@ class AuthorDetailsView(DetailView):
#TODO: add template, app urls, #TODO: add template, app urls,
def AuthorAddView(request): def AuthorAddView(request):
#context ={}
if request.method == 'POST': if request.method == 'POST':
form = AuthorForm(request.POST) form = AuthorForm(request.POST)
if form.is_valid(): if form.is_valid():
new_author = form.save() new_author = form.save()
#context['form']= form
return redirect('bookshelf:authors-detail', pk=new_author.pk) return redirect('bookshelf:authors-detail', pk=new_author.pk)
else: else:
form = AuthorForm() form = AuthorForm()
return render(request, 'bookshelf/author_add.html', {'form': form}) return render(request, 'bookshelf/author_add.html', {'form': form})
def BookAddView(request):
if request.method == 'POST':
form = BooksForm(request.POST)
if form.is_valid():
new_book = form.save()
return redirect('bookshelf:books-detail', pk=new_book.pk)
else:
form = BooksForm()
return render(request, 'bookshelf/book_add.html', {'form': form})
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