Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-12_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
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
Давид Ли
ap-12_django
Commits
90c2d2d3
Commit
90c2d2d3
authored
Feb 29, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 60
parent
97811be9
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
162 additions
and
23 deletions
+162
-23
admin.py
ap_12/accounts/admin.py
+15
-2
0005_profile.py
ap_12/accounts/migrations/0005_profile.py
+28
-0
0006_alter_profile_avatar.py
ap_12/accounts/migrations/0006_alter_profile_avatar.py
+18
-0
models.py
ap_12/accounts/models.py
+17
-0
urls.py
ap_12/accounts/urls.py
+3
-2
views.py
ap_12/accounts/views.py
+34
-2
settings.py
ap_12/core/settings.py
+5
-0
urls.py
ap_12/core/urls.py
+4
-1
user_detail.html
ap_12/templates/auth/user_detail.html
+21
-0
base.html
ap_12/templates/base.html
+3
-1
index.html
ap_12/templates/index.html
+1
-11
article_list.html
ap_12/templates/partial/article_list.html
+11
-0
pagination.html
ap_12/templates/partial/pagination.html
+1
-1
62cf6bd0938fda2f588142b86920161a.png
ap_12/uploads/avatars/62cf6bd0938fda2f588142b86920161a.png
+0
-0
no_image.jpeg
ap_12/uploads/avatars/no_image.jpeg
+0
-0
reqs.txt
reqs.txt
+1
-3
No files found.
ap_12/accounts/admin.py
View file @
90c2d2d3
from
django.contrib
import
admin
from
accounts.models
import
User
from
django.contrib.auth.admin
import
UserAdmin
from
accounts
import
models
admin
.
site
.
register
(
User
)
# admin.site.register(User)
class
ProfileInline
(
admin
.
StackedInline
):
model
=
models
.
Profile
fields
=
[
'birth_date'
,
'avatar'
]
class
MyUserAdmin
(
UserAdmin
):
inlines
=
[
ProfileInline
]
admin
.
site
.
register
(
models
.
User
,
MyUserAdmin
)
ap_12/accounts/migrations/0005_profile.py
0 → 100644
View file @
90c2d2d3
# Generated by Django 3.2.19 on 2024-02-29 13:41
from
django.conf
import
settings
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'accounts'
,
'0004_alter_user_is_active'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Profile'
,
fields
=
[
(
'id'
,
models
.
BigAutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'birth_date'
,
models
.
DateField
(
null
=
True
,
verbose_name
=
'Дата рождения'
)),
(
'avatar'
,
models
.
ImageField
(
null
=
True
,
upload_to
=
'avatars'
,
verbose_name
=
'Аватар'
)),
(
'user'
,
models
.
OneToOneField
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
settings
.
AUTH_USER_MODEL
,
verbose_name
=
'Пользователь'
)),
],
options
=
{
'verbose_name'
:
'Профиль'
,
'verbose_name_plural'
:
'Профили'
,
},
),
]
ap_12/accounts/migrations/0006_alter_profile_avatar.py
0 → 100644
View file @
90c2d2d3
# Generated by Django 3.2.19 on 2024-02-29 14:52
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'accounts'
,
'0005_profile'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'profile'
,
name
=
'avatar'
,
field
=
models
.
ImageField
(
default
=
'avatars/no_image.jpeg'
,
null
=
True
,
upload_to
=
'avatars'
,
verbose_name
=
'Аватар'
),
),
]
ap_12/accounts/models.py
View file @
90c2d2d3
...
...
@@ -18,3 +18,20 @@ class User(AbstractUser):
),
)
password
=
models
.
CharField
(
max_length
=
128
,
verbose_name
=
'password'
,
null
=
True
)
# Pillow
# sudo apt-get install libjpeg-dev zlib1g-dev
class
Profile
(
models
.
Model
):
user
=
models
.
OneToOneField
(
User
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
'Пользователь'
)
birth_date
=
models
.
DateField
(
null
=
True
,
verbose_name
=
'Дата рождения'
)
avatar
=
models
.
ImageField
(
null
=
True
,
upload_to
=
'avatars'
,
verbose_name
=
'Аватар'
,
default
=
'avatars/no_image.jpeg'
)
class
Meta
:
verbose_name
=
'Профиль'
verbose_name_plural
=
'Профили'
def
__str__
(
self
):
return
self
.
user
.
get_full_name
()
ap_12/accounts/urls.py
View file @
90c2d2d3
from
accounts
.views
import
RegisterView
from
accounts
import
views
from
django.urls
import
path
from
django.contrib.auth.views
import
LoginView
,
LogoutView
...
...
@@ -7,5 +7,6 @@ from django.contrib.auth.views import LoginView, LogoutView
urlpatterns
=
[
path
(
'login'
,
LoginView
.
as_view
(
template_name
=
'auth/login.html'
),
name
=
'login'
),
path
(
'logout'
,
LogoutView
.
as_view
(),
name
=
'logout'
),
path
(
'register'
,
RegisterView
.
as_view
(),
name
=
'register'
),
path
(
'register'
,
views
.
RegisterView
.
as_view
(),
name
=
'register'
),
path
(
'<int:id>/'
,
views
.
UserDetailView
.
as_view
(),
name
=
'profile'
),
]
ap_12/accounts/views.py
View file @
90c2d2d3
from
django.http
import
HttpRequest
from
django.urls
import
reverse
from
accounts
import
forms
from
accounts
import
forms
,
models
from
django.views
import
generic
from
django.contrib.auth
import
login
,
get_user_model
from
django.contrib.auth
import
login
,
get_user_model
,
mixins
from
django.shortcuts
import
redirect
from
django.core
import
exceptions
from
django.core.paginator
import
Paginator
class
RegisterView
(
generic
.
CreateView
):
...
...
@@ -56,3 +58,33 @@ class RegisterView(generic.CreateView):
'next'
,
self
.
request
.
GET
.
get
(
'next'
,
'index'
)
)
class
UserDetailView
(
mixins
.
LoginRequiredMixin
,
generic
.
DetailView
):
model
=
get_user_model
()
template_name
=
'auth/user_detail.html'
context_object_name
=
'user_obj'
pk_url_kwarg
=
'id'
paginate_by
=
3
paginate_orphans
=
0
def
__get_paginator
(
self
):
articles
=
self
.
object
.
articles
.
order_by
(
'-created_at'
)
return
Paginator
(
articles
,
self
.
paginate_by
,
self
.
paginate_orphans
)
def
get_context_data
(
self
,
**
kwargs
):
# Проверяем, если профиль привязанный к нашему юзеру уже есть, то ничего не делать
# Если нет, то создать пустой профиль
models
.
Profile
.
objects
.
get_or_create
(
user
=
self
.
object
)
paginator
=
self
.
__get_paginator
()
page_number
=
self
.
request
.
GET
.
get
(
'page'
,
1
)
page
=
paginator
.
get_page
(
page_number
)
return
super
()
.
get_context_data
(
**
kwargs
,
paginator
=
paginator
,
page_obj
=
page
,
is_paginated
=
page
.
has_other_pages
(),
)
ap_12/core/settings.py
View file @
90c2d2d3
...
...
@@ -137,3 +137,8 @@ LOGIN_REDIRECT_URL = LOGOUT_REDIRECT_URL = 'index'
LOGIN_URL
=
'login'
AUTH_USER_MODEL
=
'accounts.User'
# MEDIA
MEDIA_ROOT
=
BASE_DIR
/
'uploads'
MEDIA_URL
=
'/images/'
# localhost:1025/uploads/avatars/user_1.jpg
ap_12/core/urls.py
View file @
90c2d2d3
...
...
@@ -15,6 +15,8 @@ Including another URLconf
"""
from
django.contrib
import
admin
from
django.urls
import
path
,
include
from
django.conf.urls.static
import
static
from
django.conf
import
settings
from
webapp
import
views
...
...
@@ -24,4 +26,5 @@ urlpatterns = [
path
(
'articles/'
,
include
(
'webapp.urls'
)),
path
(
'accounts/'
,
include
(
'accounts.urls'
)),
]
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
ap_12/templates/auth/user_detail.html
0 → 100644
View file @
90c2d2d3
{% extends 'base.html' %}
{% block content %}
<h1>
Личная страница пользователя {{ user_obj.get_full_name }}
</h1>
{% if user_obj.profile.avatar %}
<img
src=
"{{ user_obj.profile.avatar.url }}"
width=
"250"
height=
"250"
class=
"object-fit-cover"
alt=
"user_avatar"
>
{% endif %}
<p>
Имя пользователя: {{ user_obj.username }}
</p>
<p>
Имя: {{ user_obj.first_name }}
</p>
<p>
Фамилия: {{ user_obj.last_name }}
</p>
<p>
Дата Рождения: {{ user_obj.profile.birth_date|date:'d.m.Y' }}
</p>
<p>
Почта: {{ user_obj.email }}
</p>
<h2
class=
"text-center"
>
Статьи
</h2>
{{page_obj.has_other_pages}}
{% include 'partial/article_list.html' with articles=page_obj.object_list is_paginated=page_obj.has_other_pages %}
{% endblock %}
\ No newline at end of file
ap_12/templates/base.html
View file @
90c2d2d3
...
...
@@ -35,7 +35,9 @@
<a
class=
"nav-link"
href=
"{% url 'logout' %}?next={{ request.get_full_path }}"
>
Logout
</a>
</li>
<li
class=
"nav-item"
>
<span
class=
"nav-link fw-bold"
>
Hello, {{ user.username }}
</span>
<span
class=
"nav-link fw-bold"
>
Hello,
<a
href=
"{% url 'profile' user.id %}"
class=
"text-decoration-none text-secondary"
>
{{ user.username }}
</a>
</span>
</li>
{% else %}
...
...
ap_12/templates/index.html
View file @
90c2d2d3
...
...
@@ -6,17 +6,7 @@
<p><a
href=
"{% url 'articles_add' %}"
>
Добавить
</a></p>
{% for article in articles %}
<h2>
{{ article.title }}
</h2>
<a
href=
"{% url 'articles_detail' id=article.id %}"
>
Подробнее...
</a>
<hr>
{% empty %}
<h2>
no articles yet ...
</h2>
{% endfor %}
{% include 'partial/article_list.html' %}
</div>
{% if is_paginated %}
{% include 'partial/pagination.html' %}
{% endif %}
{% endblock %}
\ No newline at end of file
ap_12/templates/partial/article_list.html
0 → 100644
View file @
90c2d2d3
{% for article in articles %}
<h2>
{{ article.title }}
</h2>
<a
href=
"{% url 'articles_detail' id=article.id %}"
>
Подробнее...
</a>
<hr>
{% empty %}
<h2>
no articles yet ...
</h2>
{% endfor %}
{% if is_paginated %}
{% include 'partial/pagination.html' %}
{% endif %}
ap_12/templates/partial/pagination.html
View file @
90c2d2d3
<div
class=
"d-flex justify-content-center"
>
<div
class=
"d-flex justify-content-center
mt-5
"
>
<nav
aria-label=
"..."
>
<ul
class=
"pagination"
>
{% if page_obj.has_previous %}
...
...
ap_12/uploads/avatars/62cf6bd0938fda2f588142b86920161a.png
0 → 100644
View file @
90c2d2d3
409 KB
ap_12/uploads/avatars/no_image.jpeg
0 → 100644
View file @
90c2d2d3
20.4 KB
reqs.txt
View file @
90c2d2d3
asgiref==3.7.2
Django==3.2.23
pytz==2023.3.post1
sqlparse==0.4.4
Pillow==10.0.0
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