Templates have been created

parent 24af37ee
from django.db import models from django.db import models
from django.urls import reverse
# Create your models here. # Create your models here.
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
age = models.IntegerField() age = models.IntegerField()
nationality = models.CharField(max_length=50) nationality = models.CharField(max_length=50)
bio = models.TextField(max_length=700) bio = models.TextField(max_length=700)
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:authors-detail', kwargs={'pk': self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length=1000) title = models.CharField(max_length=1000)
...@@ -23,4 +27,7 @@ class Books(models.Model): ...@@ -23,4 +27,7 @@ class Books(models.Model):
blurb = models.TextField() blurb = models.TextField()
def __str__(self): def __str__(self):
return '{}'.format(self.title) return '{}'.format(self.title)
\ No newline at end of file
def get_absolute_url(self):
return reverse('bookshelf:books-detail', kwargs={'pk': self.pk})
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Authors {% endblock %}
{% block content %}
<h1>Luigi's Favorite Authors: </h1>
<ul>
{% for object in authors %}
<li>
<a href="{{ object.get_absolute_url }}">{{ object }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.first_name }}{% endblock %}
{% block content %}
<h1>{{ object.first_name }} {{object.last_name}}</h1>
<ul>
<li>{{ object.age }}</li>
<li>{{ object.nationality }}</li>
<li>{{ object.bio }}</li>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<ul>
<li>{{ object.author }}</li>
<li>{{ object.publisher }}</li>
<li>{{ object.year_published }}</li>
<li>{{ object.ISBN }}</li>
<li>{{ object.blurb }}</li>
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Books {% endblock %}
{% block content %}
<h1>Luigi's Favorite Books</h1>
<ul>
{% for object in books %}
<li>
<a href="{{ object.get_absolute_url }}">{{ object.title }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} My Favorite Books and Authors {% endblock %}
{% block content %}
<h1>Welcome to Luigi's Database of Favorite Books and Authors!</h1>
<p>
I don't usually read in my freetime, but when I do, I usually
<br>read mystery or coming of age novels. John Green is an author
<br>that appeals to me.
</p>
<br>
<a href="http://127.0.0.1:8000/bookshelf/books">Books</a>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import (home_view,
BooksListView, BooksDetailView,
AuthorsListView, AuthorsDetailView
)
urlpatterns = [ urlpatterns = [
path('', index, name='index') path('home', home_view, name='index'),
path('books', BooksListView.as_view(), name='books-list'),
path('books/<int:pk>', BooksDetailView.as_view(), name='books-detail'),
path('authors', AuthorsListView.as_view(), name='authors-list'),
path('authors/<int:pk>', AuthorsDetailView.as_view(), name='authors-detail')
] ]
app_name = "bookshelf" app_name = "bookshelf"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
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 Author, Books
# Create your views here. # Create your views here.
def index(request): def home_view(request):
return HttpResponse("Hello World!") return render(request, 'bookshelf/home.html')
\ No newline at end of file
class AuthorsListView(ListView):
def get(self, request):
authors = Author.objects.all()
return render(request, 'bookshelf/authors.html', {
'authors': authors
})
class AuthorsDetailView(DetailView):
models = Author
class BooksListView(ListView):
def get(self, request):
books = Books.objects.all()
return render(request, 'bookshelf/books.html', {
'books': books
})
class BooksDetailView(DetailView):
models = Books
\ No newline at end of file
...@@ -58,7 +58,7 @@ ROOT_URLCONF = 'luigirodriguez_reading.urls' ...@@ -58,7 +58,7 @@ ROOT_URLCONF = 'luigirodriguez_reading.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': [os.path.join(BASE_DIR, 'template')],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
......
...@@ -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('bookshelf/', include('bookshelf.urls', namespace="bookshelf")),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
{% block styles %}{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
{% block scripts %}{% endblock %}
</body>
</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