added update and create views for authors and books, added new buttons in existing pages

parent cd87de66
from django.forms import ModelForm
from .models import Author, Books
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = "__all__"
class BooksForm(ModelForm):
class Meta:
model = Books
fields = "__all__"
from django.db import models from django.db import models
from django.urls import reverse
# Create your models here. # Create your models here.
...@@ -13,6 +14,10 @@ class Author(models.Model): ...@@ -13,6 +14,10 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return "{} {}".format(self.first_name, self.last_name) return "{} {}".format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse("author_details", kwargs={'pk' : self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length = 150) title = models.CharField(max_length = 150)
...@@ -24,3 +29,6 @@ class Books(models.Model): ...@@ -24,3 +29,6 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse("book_details", kwargs={'pk' : self.pk})
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Author{% endblock %}
{% block content%}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "Submit" value = "Add Author">
{%endblock%}
{% extends 'base.html' %}
{% load static %}
{% block title %}Add New Book{% endblock %}
{% block content%}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "Submit" value = "Add Book">
{%endblock%}
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
<p>{{ object.nationality }}</p> <p>{{ object.nationality }}</p>
<p>{{ object.bio }}</p> <p>{{ object.bio }}</p>
<input type = "Submit" value = "Edit Author" onclick="location.href='http://localhost:8000/authors/{{ object.pk }}/edit/'">
<p>Books by {{ object.first_name }} {{ object.last_name }} I love:</p> <p>Books by {{ object.first_name }} {{ object.last_name }} I love:</p>
{% for books in object.books_set.all %} {% for books in object.books_set.all %}
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
{% block pagelinks %} {% block pagelinks %}
<pre> <pre>
<input type = "Submit" value = "Edit Book" onclick="location.href='http://localhost:8000/books/{{ object.pk }}/edit/'">
<a href="http://localhost:8000/home/">Home</a> <a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/authors/">Authors</a> <a href="http://localhost:8000/home/">Home</a> <a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/authors/">Authors</a>
</pre> </pre>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Author{% endblock %}
{% block content%}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "Submit" value = "Save Changes">
{%endblock%}
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Book{% endblock %}
{% block content%}
<form method = "POST">
{% csrf_token %}
{{ form.as_p }}
<input type = "Submit" value = "Save Changes">
{%endblock%}
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
{% block pagelinks %} {% block pagelinks %}
<pre> <pre>
<a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/authors/">Authors</a> <a href="http://localhost:8000/books/">Books</a> <a href="http://localhost:8000/authors/">Authors</a>
<a href="http://localhost:8000/books/add">Add Book</a> <a href="http://localhost:8000/authors/add">Add Author</a>
</pre> </pre>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
from .views import BooksPageView, BooksDetailView, AuthorsPageView, AuthorsDetailView from .views import BooksPageView, BooksDetailView, AuthorsPageView, AuthorsDetailView, BooksCreateView, AuthorCreateView, BooksUpdateView, AuthorUpdateView
urlpatterns = [ urlpatterns = [
path('home/', views.Homepage, name = "home"), path('home/', views.Homepage, name = "home"),
path('books/', BooksPageView.as_view(), name = "books" ), path('books/', BooksPageView.as_view(), name = "books" ),
path('books/<int:pk>/details', BooksDetailView.as_view(), name = "books_details"), path('books/<int:pk>/details', BooksDetailView.as_view(), name = "book_details"),
path('authors/', AuthorsPageView.as_view(), name = "authors"), path('authors/', AuthorsPageView.as_view(), name = "authors"),
path('authors/<int:pk>/details', AuthorsDetailView.as_view(), name = "author_details"), path('authors/<int:pk>/details', AuthorsDetailView.as_view(), name = "author_details"),
path('books/add', BooksCreateView.as_view(), name = "add-book"),
path('authors/add', AuthorCreateView.as_view(), name = "add-author"),
path('books/<int:pk>/edit/', BooksUpdateView.as_view(), name = "edit-book"),
path('authors/<int:pk>/edit/', AuthorUpdateView.as_view(), name = "edit-author"),
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
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 django.views.generic.edit import CreateView, UpdateView
from .models import Books, Author from .models import Books, Author
# Create your views here. # Create your views here.
...@@ -10,12 +11,37 @@ def Homepage(request): ...@@ -10,12 +11,37 @@ def Homepage(request):
class BooksPageView(ListView): class BooksPageView(ListView):
model = Books model = Books
template_name = "bookshelf/books.html"
class BooksDetailView(DetailView): class BooksDetailView(DetailView):
model = Books model = Books
template_name = "bookshelf/book_details.html"
class AuthorsPageView(ListView): class AuthorsPageView(ListView):
model = Author model = Author
template_name = "bookshelf/authors.html"
class AuthorsDetailView(DetailView): class AuthorsDetailView(DetailView):
model = Author model = Author
template_name = "bookshelf/author_details.html"
class BooksCreateView(CreateView):
model = Books
fields = '__all__'
template_name = "bookshelf/add-book.html"
class BooksUpdateView(UpdateView):
model = Books
fields = '__all__'
template_name = "bookshelf/edit-book.html"
class AuthorCreateView(CreateView):
model = Author
fields = '__all__'
template_name = "bookshelf/add-author.html"
class AuthorUpdateView(UpdateView):
model = Author
fields = '__all__'
template_name = "bookshelf/edit-author.html"
\ 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