Edit Book and Edit Author has been implemented

parent fee1f85e
...@@ -14,6 +14,9 @@ class Author(models.Model): ...@@ -14,6 +14,9 @@ class Author(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse('bookshelf:authors-detail', kwargs={'pk': self.pk}) return reverse('bookshelf:authors-detail', kwargs={'pk': self.pk})
def get_edit_url(self):
return reverse('bookshelf:authors-edit', kwargs={'pk': self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=1000) title = models.CharField(max_length=1000)
......
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Author {% endblock %}
{% block content %}
<h1>Edit Author</h1>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Book {% endblock %}
{% block content %}
<h1>Edit Book</h1>
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Save Changes">
</form>
{% endblock %}
\ No newline at end of file
...@@ -4,7 +4,8 @@ from django.views.generic.list import ListView ...@@ -4,7 +4,8 @@ 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 django.views.generic.edit import CreateView, UpdateView
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from .models import Author, Books from .models import Author, Books
from .forms import AuthorForm, BookForm from .forms import AuthorForm, BookForm
...@@ -32,7 +33,7 @@ class AuthorsCreateView(CreateView): ...@@ -32,7 +33,7 @@ class AuthorsCreateView(CreateView):
class AuthorsUpdateView(UpdateView): class AuthorsUpdateView(UpdateView):
model = Author model = Author
fields = '__all__' fields = '__all__'
template_name = 'bookshelf/authors_details.html' template_name = 'bookshelf/edit-author.html'
class BooksListView(ListView): class BooksListView(ListView):
def get(self, request): def get(self, request):
...@@ -53,8 +54,15 @@ class BooksCreateView(CreateView): ...@@ -53,8 +54,15 @@ class BooksCreateView(CreateView):
class BooksUpdateView(UpdateView): class BooksUpdateView(UpdateView):
model = Books model = Books
fields = '__all__' fields = '__all__'
template_name = 'bookshelf/book_details.html' template_name = 'bookshelf/edit-book.html'
def edit_author(request, id):
author = get_object_or_404(Author, id=id)
if request.method == 'GET':
return render(request, 'bookshelf/edit-author.html',
{'form': AuthorForm(instance=author), 'id': id})
'''
def author_view(request): def author_view(request):
if request.method == 'POST': if request.method == 'POST':
form = AuthorForm(request.POST) form = AuthorForm(request.POST)
...@@ -63,7 +71,7 @@ def author_view(request): ...@@ -63,7 +71,7 @@ def author_view(request):
return redirect(AuthorsDetailView, pk=new_author.pk) return redirect(AuthorsDetailView, pk=new_author.pk)
else: else:
form = AuthorForm() form = AuthorForm()
return render(request, 'add-author.html', {'form': form}) return render(request, 'edit-author.html', {'form': form})
def book_view(request): def book_view(request):
if request.method == 'POST': if request.method == 'POST':
...@@ -73,4 +81,5 @@ def book_view(request): ...@@ -73,4 +81,5 @@ def book_view(request):
return redirect(AuthorsDetailView, pk=new_author.pk) return redirect(AuthorsDetailView, pk=new_author.pk)
else: else:
form = AuthorForm() form = AuthorForm()
return render(request, 'add-book.html', {'form': form}) return render(request, 'edit-book.html', {'form': form})
\ No newline at end of file '''
\ 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