Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
Deokhyun_Lee_reading
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Deokhyun Lee
Deokhyun_Lee_reading
Commits
64bd34c5
Commit
64bd34c5
authored
Mar 27, 2023
by
Deokhyun Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Now, FBV is converted to CBV
parent
4a9b9898
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
40 deletions
+43
-40
urls.cpython-39.pyc
bookshelf/__pycache__/urls.cpython-39.pyc
+0
-0
views.cpython-39.pyc
bookshelf/__pycache__/views.cpython-39.pyc
+0
-0
urls.py
bookshelf/urls.py
+9
-6
views.py
bookshelf/views.py
+34
-34
No files found.
bookshelf/__pycache__/urls.cpython-39.pyc
View file @
64bd34c5
No preview for this file type
bookshelf/__pycache__/views.cpython-39.pyc
View file @
64bd34c5
No preview for this file type
bookshelf/urls.py
View file @
64bd34c5
from
django.urls
import
path
from
.
import
views
from
.
views
import
HomeView
,
AuthorListView
,
AuthorDetailView
,
BookListView
,
BookDetailView
urlpatterns
=
[
path
(
'home/'
,
views
.
home
,
name
=
"home"
),
path
(
'books/'
,
views
.
books
,
name
=
"books"
),
path
(
'books/<int:pk>/details'
,
views
.
books_detail
,
name
=
"books_detail"
),
path
(
'authors/'
,
views
.
authors
,
name
=
"authors"
),
path
(
'authors/<int:pk>/details'
,
views
.
authors_detail
,
name
=
"authors_detail"
),
# for Home (landing page)
path
(
''
,
HomeView
.
as_view
(),
name
=
'home'
),
# for Authors page
path
(
'authors/'
,
AuthorListView
.
as_view
(),
name
=
'authors'
),
path
(
'authors/<int:pk>/details'
,
AuthorDetailView
.
as_view
(),
name
=
'author-detail'
),
# for Books page
path
(
'books/'
,
BookListView
.
as_view
(),
name
=
'books'
),
path
(
'books/<int:pk>/details'
,
BookDetailView
.
as_view
(),
name
=
'book-detail'
),
]
\ No newline at end of file
bookshelf/views.py
View file @
64bd34c5
from
django.http
import
HttpResponse
from
.models
import
Author
,
Books
from
datetime
import
date
from
django.template
import
loader
from
django.views
import
View
from
django.views.generic
import
ListView
,
DetailView
from
django.shortcuts
import
render
from
.models
import
Author
,
Books
# View for Home (landing page)
class
HomeView
(
View
):
def
get
(
self
,
request
):
return
render
(
request
,
'home/index.html'
)
# views for Home (landing page)
def
home
(
request
):
context
=
{}
template
=
loader
.
get_template
(
'home/index.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
# for Authors page
def
authors
(
request
):
authors
=
Author
.
objects
.
order_by
(
'first_name'
)
context
=
{
'authors'
:
authors
}
template
=
loader
.
get_template
(
'authors/index.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
# Views for Authors page
class
AuthorListView
(
ListView
):
model
=
Author
template_name
=
'authors/index.html'
context_object_name
=
'authors'
ordering
=
[
'first_name'
]
def
authors_detail
(
request
,
pk
):
author
=
Author
.
objects
.
get
(
pk
=
pk
)
# get books related to the author's id.
books
=
Books
.
objects
.
filter
(
author__pk
=
pk
)
context
=
{
'author'
:
author
,
'books'
:
books
}
template
=
loader
.
get_template
(
'authors/author.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
class
AuthorDetailView
(
DetailView
):
model
=
Author
template_name
=
'authors/author.html'
context_object_name
=
'author'
# this line for getting the param value "pk"
# returns Dict[str, Any]
def
get_context_data
(
self
,
**
kwargs
):
context
=
super
()
.
get_context_data
(
**
kwargs
)
# get by self == Author's id to get Book's foreign key that matches up.
context
[
'books'
]
=
Books
.
objects
.
filter
(
author
=
self
.
object
)
return
context
# for Books page
def
books
(
request
):
books
=
Books
.
objects
.
order_by
(
'year_published'
)
context
=
{
'books'
:
books
}
template
=
loader
.
get_template
(
'books/index.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
#
Views
for Books page
class
BookListView
(
ListView
):
model
=
Books
template_name
=
'books/index.html'
context_object_name
=
'books'
ordering
=
[
'year_published'
]
def
books_detail
(
request
,
pk
):
book
=
Books
.
objects
.
get
(
pk
=
pk
)
context
=
{
'book'
:
book
}
template
=
loader
.
get_template
(
'books/book.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
\ No newline at end of file
class
BookDetailView
(
DetailView
):
model
=
Books
template_name
=
'books/book.html'
context_object_name
=
'book'
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment