Cleaned Code

parent ae97c5a2
...@@ -16,7 +16,7 @@ class Author(models.Model): ...@@ -16,7 +16,7 @@ class Author(models.Model):
return'{} {}'.format(self.first_name, self.last_name) return'{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self): def get_absolute_url(self):
return reverse("Author_details", kwargs={"pk": self.pk}) return reverse("authordetails", kwargs={"pk": self.pk})
class Book(models.Model): class Book(models.Model):
title=models.CharField(max_length=100) title=models.CharField(max_length=100)
...@@ -40,4 +40,4 @@ class Book(models.Model): ...@@ -40,4 +40,4 @@ class Book(models.Model):
return'{}'.format(self.title) return'{}'.format(self.title)
def get_absolute_url(self): def get_absolute_url(self):
return reverse("Book_details", kwargs={'pk': self.pk}) return reverse("bookdetails", kwargs={'pk': self.pk})
\ No newline at end of file \ 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 (Home_Views, Authors_Details_Views, from .views import (HomeViews, AuthorsDetailsViews,
Authors_Views, Book_Details_Views, AuthorsViews, BookDetailsViews,
Books_Views) BooksViews)
urlpatterns = [ urlpatterns = [
path('home/', views.Home_Views, name='Home'), path('home/', views.HomeViews, name='home'),
path('authors/', Authors_Views.as_view(), name='Authors'), path('authors/', AuthorsViews.as_view(), name='authors'),
path('books/', Books_Views.as_view(), name='Books'), path('books/', BooksViews.as_view(), name='books'),
path('authors/<int:pk>/details/', Authors_Details_Views.as_view(), name='Author_details'), path('authors/<int:pk>/details/', AuthorsDetailsViews.as_view(), name='authordetails'),
path('books/<int:pk>/details/', Book_Details_Views.as_view(), name='Book_details'), path('books/<int:pk>/details/', BookDetailsViews.as_view(), name='bookdetails'),
] ]
...@@ -4,33 +4,28 @@ from django.views.generic import ListView, DetailView ...@@ -4,33 +4,28 @@ from django.views.generic import ListView, DetailView
from .models import * from .models import *
# Create your views here. # Create your views here.
def Home_Views(request): def HomeViews(request):
return render(request, 'home.html', {}) return render(request, 'home.html', {})
class Books_Views(ListView): class BooksViews(ListView):
model = Book model = Book
template_name = "books.html" template_name = "books.html"
context_object_name= 'Books_list' context_object_name= 'books_list'
class Authors_Views(ListView): class AuthorsViews(ListView):
model = Author model = Author
template_name = "authors.html" template_name = "authors.html"
context_object_name='Authors_list' context_object_name='authors_list'
class Authors_Details_Views(DetailView): class AuthorsDetailsViews(DetailView):
template_name = "author_details.html" template_name = "author_details.html"
model = Author model = Author
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(Authors_Details_Views, self).get_context_data(**kwargs) context = super(AuthorsDetailsViews, self).get_context_data(**kwargs)
context ['bookdetail'] = Book.objects.filter(author=self.get_object()) context ['bookdetail'] = Book.objects.filter(author=self.get_object())
return context return context
class Book_Details_Views(DetailView): class BookDetailsViews(DetailView):
template_name = "book_details.html" template_name = "book_details.html"
model = Book model = Book
\ No newline at end of file
def get_context_data(self, **kwargs):
context = super(Book_Details_Views, self).get_context_data(**kwargs)
context ['authordetail'] = Author.objects.filter(book=self.get_object())
return context
\ No newline at end of file
...@@ -11,10 +11,10 @@ ...@@ -11,10 +11,10 @@
<h2>{{object.bio}}</h2> <h2>{{object.bio}}</h2>
<h1>Books by {{object.first_name}} {{object.last_name}} I love:</h1> <h1>Books by {{object.first_name}} {{object.last_name}} I love:</h1>
{% for books in bookdetail %} {% for details in bookdetail %}
<li> <li>
<a href="{%url 'Book_details' books.pk %}"> <a href="{%url 'bookdetails' details.pk %}">
{{books.title}}<br> {{details.title}}<br>
</li> </li>
{%endfor%} {%endfor%}
......
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
</head> </head>
<h1>Anchi's Favorite Authors:</h1> <h1>Anchi's Favorite Authors:</h1>
{% for Author in Authors_list %} {% for author in authors_list %}
<li> <li>
<a href="{% url 'Author_details' Author.pk %}"> <a href="{% url 'author_details' author.pk %}">
{{Author.first_name}} {{Author.last_name}}<br> {{author.first_name}} {{author.last_name}}<br>
</a> </a>
</li> </li>
{% endfor %} {% endfor %}
......
...@@ -6,9 +6,7 @@ ...@@ -6,9 +6,7 @@
<title>{{object.title}}</title> <title>{{object.title}}</title>
<h1>{{object.title}}</a></h1> <h1>{{object.title}}</a></h1>
{% for Author in authordetail %} <h2><a href="{{object.author.get_absolute_url}}">{{object.author}}</a></h2>
<h2><a href="{%url 'Author_details' Author.pk %}">{{object.author}}</a></h2>
{% endfor %}
<h2>{{object.publisher}}</h2> <h2>{{object.publisher}}</h2>
<h2>{{object.year_published|date:'Y'}}</h2> <h2>{{object.year_published|date:'Y'}}</h2>
<h2>{{object.ISBN}}</h2> <h2>{{object.ISBN}}</h2>
......
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
</head> </head>
<h1>Anchi's Favorite Books:</h1> <h1>Anchi's Favorite Books:</h1>
{% for books in Books_list %} {% for book in books_list %}
<li> <li>
<a href="{%url 'Book_details' books.pk %}"> <a href="{%url 'bookdetails' book.pk %}">
{{books.title}}<br> {{book.title}}<br>
</a> </a>
</li> </li>
......
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