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
Hide 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
django.urls
import
path
from
.
import
views
from
.
views
import
HomeView
,
AuthorListView
,
AuthorDetailView
,
BookListView
,
BookDetailView
urlpatterns
=
[
urlpatterns
=
[
path
(
'home/'
,
views
.
home
,
name
=
"home"
),
# for Home (landing page)
path
(
'books/'
,
views
.
books
,
name
=
"books"
),
path
(
''
,
HomeView
.
as_view
(),
name
=
'home'
),
path
(
'books/<int:pk>/details'
,
views
.
books_detail
,
name
=
"books_detail"
),
# for Authors page
path
(
'authors/'
,
views
.
authors
,
name
=
"authors"
),
path
(
'authors/'
,
AuthorListView
.
as_view
(),
name
=
'authors'
),
path
(
'authors/<int:pk>/details'
,
views
.
authors_detail
,
name
=
"authors_detail"
),
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
django.views
import
View
from
.models
import
Author
,
Books
from
django.views.generic
import
ListView
,
DetailView
from
datetime
import
date
from
django.template
import
loader
from
django.shortcuts
import
render
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)
# Views for Authors page
def
home
(
request
):
class
AuthorListView
(
ListView
):
context
=
{}
model
=
Author
template
=
loader
.
get_template
(
'home/index.html'
)
template_name
=
'authors/index.html'
return
HttpResponse
(
template
.
render
(
context
,
request
))
context_object_name
=
'authors'
ordering
=
[
'first_name'
]
# for Authors page
class
AuthorDetailView
(
DetailView
):
def
authors
(
request
):
model
=
Author
authors
=
Author
.
objects
.
order_by
(
'first_name'
)
template_name
=
'authors/author.html'
context
=
{
'authors'
:
authors
}
context_object_name
=
'author'
template
=
loader
.
get_template
(
'authors/index.html'
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
def
authors_detail
(
request
,
pk
):
# this line for getting the param value "pk"
author
=
Author
.
objects
.
get
(
pk
=
pk
)
# returns Dict[str, Any]
# get books related to the author's id.
def
get_context_data
(
self
,
**
kwargs
):
books
=
Books
.
objects
.
filter
(
author__pk
=
pk
)
context
=
super
()
.
get_context_data
(
**
kwargs
)
context
=
{
'author'
:
author
,
'books'
:
books
}
# get by self == Author's id to get Book's foreign key that matches up.
template
=
loader
.
get_template
(
'authors/author.html'
)
context
[
'books'
]
=
Books
.
objects
.
filter
(
author
=
self
.
object
)
return
HttpResponse
(
template
.
render
(
context
,
request
))
return
context
# for Books page
#
Views
for Books page
def
books
(
request
):
class
BookListView
(
ListView
):
books
=
Books
.
objects
.
order_by
(
'year_published'
)
model
=
Books
context
=
{
'books'
:
books
}
template_name
=
'books/index.html'
template
=
loader
.
get_template
(
'books/index.html'
)
context_object_name
=
'books'
return
HttpResponse
(
template
.
render
(
context
,
request
))
ordering
=
[
'year_published'
]
def
books_detail
(
request
,
pk
):
class
BookDetailView
(
DetailView
):
book
=
Books
.
objects
.
get
(
pk
=
pk
)
model
=
Books
context
=
{
'book'
:
book
}
template_name
=
'books/book.html'
template
=
loader
.
get_template
(
'books/book.html'
)
context_object_name
=
'book'
return
HttpResponse
(
template
.
render
(
context
,
request
))
\ No newline at end of file
\ 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