Finalized and updated to implement the web pages

parent 27be1865
...@@ -60,7 +60,7 @@ ROOT_URLCONF = 'addysalto_reading.urls' ...@@ -60,7 +60,7 @@ ROOT_URLCONF = 'addysalto_reading.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
...@@ -122,6 +122,7 @@ USE_TZ = True ...@@ -122,6 +122,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = 'static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
......
...@@ -17,6 +17,6 @@ from django.contrib import admin ...@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path('bookshelf/', include('bookshelf.urls', namespace="bookshelf")), path('', include('bookshelf.urls', namespace="bookshelf")),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
...@@ -4,10 +4,16 @@ from .models import Author, Books ...@@ -4,10 +4,16 @@ from .models import Author, Books
class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
model = Author model = Author
search_fields = ('first_name', 'last_name')
list_display = ('first_name', 'last_name', 'age', 'nationality', 'bio')
class BooksAdmin(admin.ModelAdmin): class BooksAdmin(admin.ModelAdmin):
model = Books model = Books
search_fields = ('title', 'author')
list_display = ('title', 'author', 'publisher', 'year_published', 'ISBN', 'blurb')
admin.site.register(Author, AuthorAdmin) admin.site.register(Author, AuthorAdmin)
......
from django.db import models from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
import datetime
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=100) first_name = models.CharField(max_length=100, default='')
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100, default='')
age = models.PositiveSmallIntegerField() age = models.PositiveSmallIntegerField(default='0')
nationality = models.CharField(max_length=100) nationality = models.CharField(max_length=100, default='')
bio = models.TextField(max_length=700) bio = models.TextField(max_length=700, default='')
def __str__(self):
return '{} {}'.format(self.first_name, self.last_name)
def get_absolute_url(self):
return reverse('bookshelf:authors-detail', kwargs={'pk': self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255, default='')
author = models.ForeignKey(Author, on_delete=models.CASCADE) author = models.ForeignKey(Author, on_delete=models.CASCADE)
publisher = models.CharField(max_length=100) publisher = models.CharField(max_length=100, default='')
year_published = YEAR_PUBLISHED_CHOICES = [(r,r) for r in range(1454, datetime.date.today().year+1)]
ISBN = models.IntegerField(max_length=13) year_published = models.PositiveIntegerField(choices=YEAR_PUBLISHED_CHOICES,
blurb = models.TextField() default=datetime.datetime.today().year)
\ No newline at end of file ISBN = models.PositiveIntegerField(
validators=[MaxValueValidator(9999999999999),
MinValueValidator(0000000000000)],
default=1000000000000)
blurb = models.TextField(default='')
def __str__(self):
return '{} by {}, {}'.format(self.title, self.author, self.year_published)
def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk': self.pk})
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import index, homepage, BookListView, BookDetailView, AuthorListView, AuthorDetailView
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', index, name='index'),
path('home', homepage, name='home'),
path('books', BookListView.as_view(), name='books-list'),
path('books/<int:pk>/details', BookDetailView.as_view(), name='books-detail'),
path('authors', AuthorListView.as_view(), name='authors-list'),
path('authors/<int:pk>/details', AuthorDetailView.as_view(), name='authors-detail'),
] ]
app_name = "bookshelf" app_name = "bookshelf"
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.views import View
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Books, Author
def index(request): def index(request):
return HttpResponse('Hello World! This came from the index view') return HttpResponse('')
\ No newline at end of file
def homepage(request):
return render(request, 'bookshelf/home.html')
class BookListView(ListView):
model = Books
template_name = 'bookshelf/books.html'
class BookDetailView(DetailView):
model = Books
template_name = 'bookshelf/book_details.html'
class AuthorListView(ListView):
model = Author
template_name = 'bookshelf/authors.html'
class AuthorDetailView(DetailView):
model = Author
template_name = 'bookshelf/author_details.html'
def get_context_data(self,**kwargs):
context = super ().get_context_data (**kwargs)
context['books'] = Books.objects.all()
return context
\ 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