Complete add htmls alongside views urls, modified model

parent 19d79024
from django.db import models from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator, MinLengthValidator, MaxLengthValidator, int_list_validator from django.core.validators import MinValueValidator, MaxValueValidator, MinLengthValidator, MaxLengthValidator, int_list_validator
import datetime import datetime
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
default_string = "" default_string = ""
...@@ -13,6 +14,9 @@ class Author(models.Model): ...@@ -13,6 +14,9 @@ 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('bookshelf:author_details', kwargs={'author_id': self.id})
class Books(models.Model): class Books(models.Model):
default_string = "" default_string = ""
title = models.CharField(max_length = 200, default = default_string) title = models.CharField(max_length = 200, default = default_string)
...@@ -24,3 +28,6 @@ class Books(models.Model): ...@@ -24,3 +28,6 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return '{}'.format(self.title) return '{}'.format(self.title)
def get_absolute_url(self):
return reverse('bookshelf:book_details', kwargs={'book_id': self.id})
\ No newline at end of file
{% 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">
</form>
{% endblock %}
\ No newline at end of file
{% 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">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
\ No newline at end of file
...@@ -11,5 +11,7 @@ ...@@ -11,5 +11,7 @@
<br /> <br />
<a href = "../books/">Books</a> <a href = "../books/">Books</a>
<a href = "../authors/">Authors</a> <a href = "../authors/">Authors</a>
<a href = "../books/add">Add Book</a>
<a href = "../authors/add">Add Authors</a>
</p> </p>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -7,6 +7,10 @@ urlpatterns = [ ...@@ -7,6 +7,10 @@ urlpatterns = [
path('authors/', views.author_view.as_view(), name='authors'), path('authors/', views.author_view.as_view(), name='authors'),
path('books/<int:book_id>/details/', views.bookdetails_view.as_view(), name='book_details'), path('books/<int:book_id>/details/', views.bookdetails_view.as_view(), name='book_details'),
path('authors/<int:author_id>/details/', views.authordetails_view.as_view(), name='author_details'), path('authors/<int:author_id>/details/', views.authordetails_view.as_view(), name='author_details'),
path('books/add', views.addbooks_view.as_view(), name='add_book'),
path('authors/add', views.addauthors_view.as_view(), name='add_author'),
#path('books//<int:book_id>/edit/', views_editbook_view.as_view(), name='edit_book'),
#path('books//<int:author_id>/edit/', views_editauthor_view.as_view(), name='edit_author'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.views.generic.edit import CreateView
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
...@@ -37,4 +38,14 @@ class authordetails_view(View): ...@@ -37,4 +38,14 @@ class authordetails_view(View):
"author": displayauthor, "author": displayauthor,
"books": books "books": books
} }
return render(request, 'bookshelf/author_details.html', context) return render(request, 'bookshelf/author_details.html', context)
\ No newline at end of file
class addbooks_view(CreateView):
model = Books
fields = '__all__'
template_name = 'bookshelf/add-book.html'
class addauthors_view(CreateView):
model = Author
fields = '__all__'
template_name = 'bookshelf/add-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