Commit a114bab8 authored by Jan Ericsson Ong Ang's avatar Jan Ericsson Ong Ang

Fixed details for books and authors

parent b9e8b471
Pipeline #3116 failed with stages
...@@ -2,22 +2,28 @@ from django.db import models ...@@ -2,22 +2,28 @@ from django.db import models
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=50, default="") first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50, default="") last_name = models.CharField(max_length=50)
age = models.IntegerField(default=0) age = models.IntegerField(default=0)
nationality = models.CharField(max_length=50, default="") nationality = models.CharField(max_length=50)
bio = models.TextField(max_length=700, default="") bio = models.TextField(max_length=700)
def __str__(self): def __str__(self):
return self.first_name + " " + self.last_name return self.first_name + " " + self.last_name
def get_absolute_url(self):
return reverse('product_change', kwargs={'id': self.id})
#https://stackoverflow.com/questions/70442828/django-object-list-from-listview-with-two-models%20#}%20{%%20for%20object%20in%20author.books_set.all
# for absolute url and _set_all
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=50, default="") title = models.CharField(max_length=50)
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
publisher = models.CharField(max_length=50, default="") publisher = models.CharField(max_length=50)
year_published = models.IntegerField(default=0) year_published = models.IntegerField(default=0)
ISBN = models.IntegerField(default=0) ISBN = models.IntegerField(default=0)
blurb = models.TextField(default="") blurb = models.TextField(max_length=10000)
def __str__(self): def __str__(self):
return self.title return self.title
\ No newline at end of file
...@@ -10,7 +10,12 @@ ...@@ -10,7 +10,12 @@
<p>{{ author.bio }}</p> <p>{{ author.bio }}</p>
<p>Books by {{ author.first_name }} {{ author.last_name }} I love</p> <p>Books by {{ author.first_name }} {{ author.last_name }} I love</p>
<a href="/bookshelf/books/<int:pk>/details/">Home</a><br>
{% for authbook in author.books_set.all %}
<a href="/bookshelf/authors/{{ authbook.id }}/details/">{{ authbook }}<br></a>
{% endfor %}
<br>
<a href="/bookshelf/">Home</a>&nbsp;&nbsp; <a href="/bookshelf/">Home</a>&nbsp;&nbsp;
<a href="/bookshelf/books/">Books</a>&nbsp;&nbsp; <a href="/bookshelf/books/">Books</a>&nbsp;&nbsp;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<br> <br>
{% for author in author_data %} {% for author in author_data %}
<a href="/bookshelf/authors/{{ author.pk }}/details/">{{ author.first_name }} {{ author.last_name }}</a> <a href="/bookshelf/authors/{{ author.id }}/details/">{{ author.first_name }} {{ author.last_name }}</a>
<br> <br>
{% endfor %} {% endfor %}
<br> <br>
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
{% block content %} {% block content %}
{% block header %}<h1>{{ books.title }}</h1>{% endblock %} {% block header %}<h1>{{ books.title }}</h1>{% endblock %}
<a href="/bookshelf/authors/{{ books.author.pk }}/details/">{{ books.author.pk }}</a><br> <a href="/bookshelf/authors/{{ author.books.id }}/details/">{{ books.author }}</a><br>
<p>{{ books.publisher }}</p> <p>{{ books.publisher }}</p>
<p>{{ books.year_published }}</p> <p>{{ books.year_published }}</p>
<p>{{ books.year_ISBN }}</p> <p>{{ books.ISBN }}</p>
<p>{{ books.year_blurb }}</p> <p>{{ books.blurb }}</p>
<a href="/bookshelf/">Home</a>&nbsp;&nbsp; <a href="/bookshelf/">Home</a>&nbsp;&nbsp;
<a href="/bookshelf/books/">Books</a>&nbsp;&nbsp; <a href="/bookshelf/books/">Books</a>&nbsp;&nbsp;
......
...@@ -5,10 +5,9 @@ ...@@ -5,10 +5,9 @@
{% block content %} {% block content %}
{% block header %}<h1>{{ nickname }}'s Favorite Books:</h1>{% endblock %} {% block header %}<h1>{{ nickname }}'s Favorite Books:</h1>{% endblock %}
<br>
{% for book in book_data %} {% for book in book_data %}
<a href="/bookshelf/books/{{ book.pk }}/details/">{{ book.title }}</a> <a href="/bookshelf/books/{{ book.id }}/details/">{{ book.title }}</a>
<br> <br>
{% endfor %} {% endfor %}
<br> <br>
......
...@@ -7,9 +7,9 @@ from .views import HomeView, AuthorView, AuthorDetailView, BooksView, BookDetail ...@@ -7,9 +7,9 @@ from .views import HomeView, AuthorView, AuthorDetailView, BooksView, BookDetail
urlpatterns = [ urlpatterns = [
path('', views.HomeView, name='home'), path('', views.HomeView, name='home'),
path('authors/', AuthorView.as_view(), name='authors'), path('authors/', AuthorView.as_view(), name='authors'),
path('authors/<int:pk>/details/', AuthorDetailView.as_view(), name='author-detail'), path('authors/<int:id>/details/', AuthorDetailView.as_view(), name='author-detail'),
path('books/', BooksView.as_view(), name='books'), path('books/', BooksView.as_view(), name='books'),
path('books/<int:pk>/details/', BookDetailView.as_view(), name='books-detail'), path('books/<int:id>/details/', BookDetailView.as_view(), name='books-detail'),
] ]
app_name = 'bookshelf' app_name = 'bookshelf'
\ No newline at end of file
...@@ -10,24 +10,29 @@ def HomeView(request): ...@@ -10,24 +10,29 @@ def HomeView(request):
return render(request, 'bookshelf/home.html', {'nickname': 'Jan'}) return render(request, 'bookshelf/home.html', {'nickname': 'Jan'})
class AuthorView(ListView): class AuthorView(ListView):
model = Author
queryset = Author.objects.all()
def get(self, request): def get(self, request):
model = Author
author_data = Author.objects.all().values() author_data = Author.objects.all().values()
return render(request, 'bookshelf/authors.html', {'nickname': 'Jan', 'author_data': author_data}) return render(request, 'bookshelf/authors.html', {'nickname': 'Jan', 'author_data': author_data})
class AuthorDetailView(DetailView): class AuthorDetailView(DetailView):
pk_url_kwarg = 'id'
model = Author model = Author
model_data = Author queryset = Author.objects.all()
template_name = 'bookshelf/author_details.html' template_name = 'bookshelf/author_details.html'
context_object_name = 'author'
class BooksView(ListView): class BooksView(ListView):
model = Books
queryset = Books.objects.all()
def get(self, request): def get(self, request):
model = Books
book_data = Books.objects.all().values() book_data = Books.objects.all().values()
return render(request, 'bookshelf/books.html', {'nickname': 'Jan', 'book_data': book_data}) return render(request, 'bookshelf/books.html', {'nickname': 'Jan', 'book_data': book_data})
class BookDetailView(DetailView): class BookDetailView(DetailView):
pk_url_kwarg = 'id'
model = Books model = Books
model_data = Books queryset = Books.objects.all()
template_name = 'bookshelf/books_details.html' template_name = 'bookshelf/book_details.html'
\ No newline at end of file context_object_name = 'books'
\ 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