Commit 3bd82eb4 authored by Cheska Hung's avatar Cheska Hung

Able to Add Author and Books

parent 7da0a9d7
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
from django.urls import path from django.urls import path
from . import views from . import views
from .views import (HomeView, BooksListView, BooksDetailView, from .views import (HomeView, BooksListView, BooksDetailView,
AuthorListView, AuthorDetailView) AuthorListView, AuthorDetailView, BookAddView,
AuthorAddView)
urlpatterns = [ urlpatterns = [
...@@ -13,6 +14,9 @@ urlpatterns = [ ...@@ -13,6 +14,9 @@ urlpatterns = [
path('authors/', AuthorListView.as_view(), name='author'), path('authors/', AuthorListView.as_view(), name='author'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), path('authors/<int:pk>/details', AuthorDetailView.as_view(),
name='author-detail'), name='author-detail'),
path('books/add', BookAddView.as_view(), name='author'),
path('authors/add', AuthorAddView.as_view(), name='author'),
] ]
app_name = 'bookshelf'
from django.shortcuts import render from django.shortcuts import render
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 import generic
from .models import Books from .models import Books
from .models import Author from .models import Author
...@@ -29,3 +30,23 @@ class AuthorDetailView(DetailView): ...@@ -29,3 +30,23 @@ class AuthorDetailView(DetailView):
queryset = Author.objects.all() queryset = Author.objects.all()
template_name = 'authors_details.html' template_name = 'authors_details.html'
context_object_name = 'authors' context_object_name = 'authors'
class BookAddView(generic.CreateView):
model = Books
fields = '__all__'
template_name = 'add-book.html'
def get_success(self):
return reverse('bookshelf:bookdetailview', kwarg={'pk': self.object.id},
current_app=self.request.resolver_match.namespace)
class AuthorAddView(generic.CreateView):
model = Author
fields = '__all__'
template_name = 'add-author.html'
def get_success(self):
return reverse('bookshelf:authordetailview', kwarg={'pk': self.object.id},
current_app=self.request.resolver_match.namespace)
\ No newline at end of file
...@@ -17,6 +17,6 @@ from django.contrib import admin ...@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('', include('bookshelf.urls', namespace="")), path('', include('bookshelf.urls', namespace='bookshelf')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
{% extends "base.html" %}
{% load static %}
{% block content %}
<title> Add New Author</title>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Author"
</form>
{% endblock content %}
\ No newline at end of file
{% extends "base.html" %}
{% load static %}
{% block content %}
<title> Add New Book</title>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Book"
</form>
{% endblock content %}
\ No newline at end of file
...@@ -14,4 +14,9 @@ ...@@ -14,4 +14,9 @@
<a href="/books">Books</a> <a href="/books">Books</a>
<a href="/authors">Authors</a> <a href="/authors">Authors</a>
<a href="/home">Home</a> <a href="/home">Home</a>
<br>
<br>
<a href="/books/"{{ object.id }}/edit/">
<input type="button" value="Edit Book">
</a>"
{% endblock content %} {% endblock content %}
\ No newline at end of file
...@@ -11,6 +11,8 @@ I like reading fantasy novels ...@@ -11,6 +11,8 @@ I like reading fantasy novels
</body> </body>
<br><br> <br><br>
<a href="/books">Books</a> <a href="/books">Books</a>
<a href="/books/add">Add Book</a>
<a href="/authors">Authors</a> <a href="/authors">Authors</a>
<a href="/authors/add">Add Author</a>
{% endblock content %} {% endblock content %}
\ 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