Commit 733e2684 authored by Joei Yucoco's avatar Joei Yucoco

Added working authors/add/ url

parent 74aba777
No preview for this file type
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']
class BooksForm(forms.ModelForm):
class Meta:
model = Books
fields = ['title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb']
<!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 Author">
</form>
{% endblock %}
from django.urls import path from django.urls import path
from .views import HomeView, BooksView, BookDetailsView, AuthorsView, AuthorDetailsView from .views import HomeView, BooksView, BookDetailsView, AuthorsView, AuthorDetailsView, AuthorAddView
urlpatterns = [ urlpatterns = [
path('home/', HomeView, name='home'), path('home/', HomeView, name='home'),
...@@ -7,6 +7,7 @@ urlpatterns = [ ...@@ -7,6 +7,7 @@ urlpatterns = [
path('books/<int:pk>/details/', BookDetailsView.as_view(), name='books-detail'), path('books/<int:pk>/details/', BookDetailsView.as_view(), name='books-detail'),
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'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.views import View from django.views import View
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 .forms import AuthorForm, BooksForm
from .models import Author, Books from .models import Author, Books
...@@ -29,4 +30,19 @@ class AuthorsView(ListView): ...@@ -29,4 +30,19 @@ class AuthorsView(ListView):
class AuthorDetailsView(DetailView): class AuthorDetailsView(DetailView):
model = Author model = Author
template_name = 'bookshelf/author_details.html' template_name = 'bookshelf/author_details.html'
#TODO: add template, app urls,
def AuthorAddView(request):
#context ={}
if request.method == 'POST':
form = AuthorForm(request.POST)
if form.is_valid():
new_author = form.save()
#context['form']= form
return redirect('bookshelf:authors-detail', pk=new_author.pk)
else:
form = AuthorForm()
return render(request, 'bookshelf/author_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