Commit fa5ac0e4 authored by Jan Ericsson Ong Ang's avatar Jan Ericsson Ong Ang

edited the 4 new html files for the forms to work

parent a0de761a
...@@ -7,15 +7,15 @@ ...@@ -7,15 +7,15 @@
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<label>First Name: </label> <label>First Name: </label>
<input id="first_name" type="text" name="first_name" max length="50"/> <input id="first_name" type="text" name="first_name" max length="50"/><br>
<label>Last Name: </label> <label>Last Name: </label>
<input id="last_name" type="text" name="last_name" max length="100"/> <input id="last_name" type="text" name="last_name" max length="100"/><br>
<label>Age: </label> <label>Age: </label>
<input id="age" type="number" name="age"/> <input id="age" type="number" name="age"/><br>
<label>Nationality: </label> <label>Nationality: </label>
<input id="nationality" type="text" name="nationality"/> <input id="nationality" type="text" name="nationality"/><br>
<label>Bio: </label> <label>Bio: </label>
<input id="bio" type="text" name="bio"/> <input id="bio" type="text" name="bio"/><br>
</form> </form>
<button type="button"> <button type="button">
......
...@@ -7,17 +7,17 @@ ...@@ -7,17 +7,17 @@
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<label>Title: </label> <label>Title: </label>
<input id="title" type="text" name="title" max length="50"/> <input id="title" type="text" name="title" max length="50"/><br>
<label>Author: </label> <label>Author: </label>
<input id="author" type="text" name="author" max length="100"/> <input id="author" type="text" name="author" max length="100"/><br>
<label>Publisher: </label> <label>Publisher: </label>
<input id="publisher" type="text" name="publisher"/> <input id="publisher" type="text" name="publisher"/><br>
<label>Year Published: </label> <label>Year Published: </label>
<input id="year_published" type="number" name="year_published"/> <input id="year_published" type="number" name="year_published"/><br>
<label>ISBN: </label> <label>ISBN: </label>
<input id="ISBN" type="number" name="ISBN"/> <input id="ISBN" type="number" name="ISBN"/><br>
<label>Blurb: </label> <label>Blurb: </label>
<input id="blurb" type="text" name="blurb"/> <input id="blurb" type="text" name="blurb"/><br>
</form> </form>
<button type="button"> <button type="button">
......
...@@ -10,15 +10,15 @@ ...@@ -10,15 +10,15 @@
<p>{{ author.bio }}</p> <p>{{ author.bio }}</p>
<button type="button"> <button type="button">
<a href="/bookshelf/books/{{ book.id }}/edit/"> <a href="/bookshelf/authors/{{ author.id }}/edit/">
Edit Book Edit Author
</a> </a>
</button><br> </button><br>
<p>Books by {{ author.first_name }} {{ author.last_name }} I love</p> <p>Books by {{ author.first_name }} {{ author.last_name }} I love</p>
{% for authbook in author.books_set.all %} {% for authbook in author.books_set.all %}
<a href="/bookshelf/authors/{{ authbook.id }}/details/">{{ authbook }}<br></a> <a href="/bookshelf/books/{{ authbook.id }}/details/">{{ authbook }}<br></a>
{% endfor %} {% endfor %}
<br> <br>
......
...@@ -7,15 +7,15 @@ ...@@ -7,15 +7,15 @@
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<label>First Name: </label> <label>First Name: </label>
<input id="first_name" type="text" name="first_name" max length="50"/> <input id="first_name" type="text" name="first_name" max length="50"/><br>
<label>Last Name: </label> <label>Last Name: </label>
<input id="last_name" type="text" name="last_name" max length="100"/> <input id="last_name" type="text" name="last_name" max length="100"/><br>
<label>Age: </label> <label>Age: </label>
<input id="age" type="number" name="age"/> <input id="age" type="number" name="age"/><br>
<label>Nationality: </label> <label>Nationality: </label>
<input id="nationality" type="text" name="nationality"/> <input id="nationality" type="text" name="nationality"/><br>
<label>Bio: </label> <label>Bio: </label>
<input id="bio" type="text" name="bio"/> <input id="bio" type="text" name="bio"/><br>
</form> </form>
<button type="button"> <button type="button">
......
...@@ -7,17 +7,17 @@ ...@@ -7,17 +7,17 @@
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<label>Title: </label> <label>Title: </label>
<input id="title" type="text" name="title" max length="50"/> <input id="title" type="text" name="title" max length="50"/><br>
<label>Author: </label> <label>Author: </label>
<input id="author" type="text" name="author" max length="100"/> <input id="author" type="text" name="author" max length="100"/><br>
<label>Publisher: </label> <label>Publisher: </label>
<input id="publisher" type="text" name="publisher"/> <input id="publisher" type="text" name="publisher"/><br>
<label>Year Published: </label> <label>Year Published: </label>
<input id="year_published" type="number" name="year_published"/> <input id="year_published" type="number" name="year_published"/><br>
<label>ISBN: </label> <label>ISBN: </label>
<input id="ISBN" type="number" name="ISBN"/> <input id="ISBN" type="number" name="ISBN"/><br>
<label>Blurb: </label> <label>Blurb: </label>
<input id="blurb" type="text" name="blurb"/> <input id="blurb" type="text" name="blurb"/><br>
</form> </form>
<button type="button"> <button type="button">
......
from django.urls import path from django.urls import path
from . import views from . import views
from .views import HomeView, AuthorView, AuthorDetailView, BooksView, BookDetailView from .views import HomeView, AuthorView, AuthorDetailView, BooksView, BookDetailView, AuthorCreateView, AuthorUpdateView, BooksCreateView, BooksUpdateView
# url for bookshelf # url for bookshelf
...@@ -8,12 +8,12 @@ urlpatterns = [ ...@@ -8,12 +8,12 @@ urlpatterns = [
path('', views.HomeView, name='home'), path('', views.HomeView, name='home'),
path('authors/', AuthorView.as_view(), name='authors'), path('authors/', AuthorView.as_view(), name='authors'),
path('authors/<int:id>/details/', AuthorDetailView.as_view(), name='author-detail'), path('authors/<int:id>/details/', AuthorDetailView.as_view(), name='author-detail'),
path('authors/<int:id>/add/', AuthorDetailView.as_view(), name='add-author'), path('authors/<int:id>/add/', AuthorCreateView.as_view(), name='add-author'),
path('authors/<int:id>/edit/', AuthorDetailView.as_view(), name='edit-author'), path('authors/<int:id>/edit/', AuthorUpdateView.as_view(), name='edit-author'),
path('books/', BooksView.as_view(), name='books'), path('books/', BooksView.as_view(), name='books'),
path('books/<int:id>/details/', BookDetailView.as_view(), name='books-detail'), path('books/<int:id>/details/', BookDetailView.as_view(), name='books-detail'),
path('books/<int:id>/add/', BookDetailView.as_view(), name='add-book'), path('books/<int:id>/add/', BooksCreateView.as_view(), name='add-book'),
path('books/<int:id>/edit/', BookDetailView.as_view(), name='edit-book'), path('books/<int:id>/edit/', BooksUpdateView.as_view(), name='edit-book'),
] ]
app_name = 'bookshelf' app_name = 'bookshelf'
\ No newline at end of file
...@@ -2,8 +2,9 @@ from django.shortcuts import render ...@@ -2,8 +2,9 @@ from django.shortcuts import render
from django.views import View from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Author, Books from .models import Author, Books
from.forms import AuthorForm, BookForm from .forms import AuthorForm, BookForm
# Create your views here. # Create your views here.
...@@ -12,23 +13,26 @@ def HomeView(request): ...@@ -12,23 +13,26 @@ def HomeView(request):
return render(request, 'bookshelf/home.html', {'nickname': 'Jan'}) return render(request, 'bookshelf/home.html', {'nickname': 'Jan'})
#CBV #CBV
#Author
class AuthorView(ListView): class AuthorView(ListView):
pk_url_kwarg = 'id'
model = Author model = Author
queryset = Author.objects.all() queryset = Author.objects.all()
def get(self, request): def get(self, request):
author_data = Author.objects.all().values() author_data = Author.objects.all().values()
return render(request, 'bookshelf/authors.html', {'nickname': 'Jan', 'author_data': author_data}) return render(request, 'bookshelf/authors.html', {'nickname': 'Jan', 'author_data': author_data})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form']= AuthorForm()
return context
def post(self, request, *args, **kwargs):
form = AuthorForm(request.POST)
if form.is_valid():
return self.get(request, *args, **kwargs)
else:
return render(request, 'bookshelf/author_details.html', {'form': form})
class AuthorCreateView(CreateView):
pk_url_kwarg = 'id'
model = Author
fields = "__all__"
template_name = 'bookshelf/add-author.html'
class AuthorUpdateView(UpdateView):
pk_url_kwarg = 'id'
model = Author
fields = "__all__"
template_name = 'bookshelf/edit-author.html'
class AuthorDetailView(DetailView): class AuthorDetailView(DetailView):
pk_url_kwarg = 'id' pk_url_kwarg = 'id'
...@@ -37,22 +41,26 @@ class AuthorDetailView(DetailView): ...@@ -37,22 +41,26 @@ class AuthorDetailView(DetailView):
template_name = 'bookshelf/author_details.html' template_name = 'bookshelf/author_details.html'
context_object_name = 'author' context_object_name = 'author'
#Books
class BooksView(ListView): class BooksView(ListView):
pk_url_kwarg = 'id'
model = Books model = Books
queryset = Books.objects.all() queryset = Books.objects.all()
def get(self, request): def get(self, request):
book_data = Books.objects.all().values() book_data = Books.objects.all().values()
return render(request, 'bookshelf/books.html', {'nickname': 'Jan', 'book_data': book_data}) return render(request, 'bookshelf/books.html', {'nickname': 'Jan', 'book_data': book_data})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) class BooksCreateView(CreateView):
context['form']= BookForm() pk_url_kwarg = 'id'
return context model = Books
def post(self, request, *args, **kwargs): fields = "__all__"
form = BookForm(request.POST) template_name = 'bookshelf/add-book.html'
if form.is_valid():
return self.get(request, *args, **kwargs) class BooksUpdateView(UpdateView):
else: pk_url_kwarg = 'id'
return render(request, 'bookshelf/book_details.html', {'form': form}) model = Books
fields = "__all__"
template_name = 'bookshelf/edit-book.html'
class BookDetailView(DetailView): class BookDetailView(DetailView):
pk_url_kwarg = 'id' pk_url_kwarg = 'id'
......
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