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
0edc3494
Commit
0edc3494
authored
Mar 04, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 61
parent
90c2d2d3
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
179 additions
and
5 deletions
+179
-5
forms.py
ap_12/accounts/forms.py
+50
-1
urls.py
ap_12/accounts/urls.py
+3
-1
views.py
ap_12/accounts/views.py
+62
-2
settings.py
ap_12/core/settings.py
+4
-1
change_password.html
ap_12/templates/auth/change_password.html
+40
-0
user_detail.html
ap_12/templates/auth/user_detail.html
+3
-0
user_update.html
ap_12/templates/auth/user_update.html
+17
-0
capibara.jpg
ap_12/uploads/avatars/capibara.jpg
+0
-0
No files found.
ap_12/accounts/forms.py
View file @
0edc3494
from
django.contrib.auth
import
get_user_model
from
django.contrib.auth.forms
import
UserCreationForm
from
django.contrib.auth.forms
import
UserCreationForm
,
PasswordChangeForm
from
django
import
forms
from
django.forms
import
widgets
from
accounts.models
import
Profile
class
RegisterForm
(
UserCreationForm
):
...
...
@@ -28,3 +31,49 @@ class SecondaryRegisterForm(UserCreationForm):
class
Meta
(
UserCreationForm
.
Meta
):
model
=
get_user_model
()
fields
=
(
'id'
,
'password1'
,
'password2'
)
class
UserUpdateForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
get_user_model
()
fields
=
(
'first_name'
,
'last_name'
,
'email'
)
widgets
=
{
'first_name'
:
widgets
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
'last_name'
:
widgets
.
TextInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
'email'
:
widgets
.
EmailInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
}
class
ProfileUpdateForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Profile
fields
=
(
'birth_date'
,
'avatar'
)
widgets
=
{
'birth_date'
:
widgets
.
DateInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
'type'
:
'date'
,
}),
'avatar'
:
widgets
.
FileInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
}
class
ChangePasswordForm
(
PasswordChangeForm
):
widgets
=
{
'new_password1'
:
widgets
.
PasswordInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
'new_password2'
:
widgets
.
PasswordInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
'old_password'
:
widgets
.
PasswordInput
(
attrs
=
{
'class'
:
'form-control mb-3'
,
}),
}
ap_12/accounts/urls.py
View file @
0edc3494
...
...
@@ -8,5 +8,7 @@ urlpatterns = [
path
(
'login'
,
LoginView
.
as_view
(
template_name
=
'auth/login.html'
),
name
=
'login'
),
path
(
'logout'
,
LogoutView
.
as_view
(),
name
=
'logout'
),
path
(
'register'
,
views
.
RegisterView
.
as_view
(),
name
=
'register'
),
path
(
'<int:id>/'
,
views
.
UserDetailView
.
as_view
(),
name
=
'profile'
),
path
(
'change_password'
,
views
.
ChangePasswordView
.
as_view
(),
name
=
'change_password'
),
path
(
'<int:id>'
,
views
.
UserDetailView
.
as_view
(),
name
=
'profile'
),
path
(
'update'
,
views
.
UserUpdateView
.
as_view
(),
name
=
'user_update'
),
]
ap_12/accounts/views.py
View file @
0edc3494
from
django.db.models.base
import
Model
as
Model
from
django.db.models.query
import
QuerySet
from
django.http
import
HttpRequest
from
django.urls
import
reverse
from
django.urls
import
reverse
,
reverse_lazy
from
accounts
import
forms
,
models
from
django.views
import
generic
from
django.contrib.auth
import
login
,
get_user_model
,
mixins
from
django.contrib.auth
import
login
,
get_user_model
,
mixins
,
update_session_auth_hash
,
views
from
django.shortcuts
import
redirect
from
django.core
import
exceptions
from
django.core.paginator
import
Paginator
from
django.contrib.auth.mixins
import
LoginRequiredMixin
class
RegisterView
(
generic
.
CreateView
):
...
...
@@ -88,3 +91,60 @@ class UserDetailView(mixins.LoginRequiredMixin, generic.DetailView):
page_obj
=
page
,
is_paginated
=
page
.
has_other_pages
(),
)
class
UserUpdateView
(
LoginRequiredMixin
,
generic
.
UpdateView
):
model
=
get_user_model
()
form_class
=
forms
.
UserUpdateForm
template_name
=
'auth/user_update.html'
context_object_name
=
'user_obj'
pk_url_kwarg
=
'id'
def
get_context_data
(
self
,
**
kwargs
):
if
'profile_form'
not
in
kwargs
:
kwargs
[
'profile_form'
]
=
self
.
get_profile_form
()
return
super
()
.
get_context_data
(
**
kwargs
)
def
get_profile_form
(
self
):
kwargs
=
{
'instance'
:
self
.
object
.
profile
}
if
self
.
request
.
method
==
'POST'
:
kwargs
[
'data'
]
=
self
.
request
.
POST
kwargs
[
'files'
]
=
self
.
request
.
FILES
return
forms
.
ProfileUpdateForm
(
**
kwargs
)
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
self
.
object
=
self
.
get_object
()
user_form
=
self
.
get_form
()
profile_form
=
self
.
get_profile_form
()
if
user_form
.
is_valid
()
and
profile_form
.
is_valid
():
return
self
.
form_valid
(
user_form
,
profile_form
)
return
self
.
form_invalid
(
user_form
,
profile_form
)
def
form_valid
(
self
,
user_form
,
profile_form
):
response
=
super
()
.
form_valid
(
user_form
)
profile_form
.
save
()
return
response
def
form_invalid
(
self
,
user_form
,
profile_form
):
context
=
self
.
get_context_data
(
form
=
user_form
,
profile_form
=
profile_form
)
return
self
.
render_to_response
(
context
)
def
get_success_url
(
self
):
return
reverse
(
'profile'
,
kwargs
=
{
'id'
:
self
.
object
.
id
})
def
get_object
(
self
,
queryset
=
None
):
return
self
.
request
.
user
class
ChangePasswordView
(
views
.
PasswordChangeView
):
form_class
=
forms
.
ChangePasswordForm
template_name
=
'auth/change_password.html'
def
get_success_url
(
self
):
return
reverse_lazy
(
'profile'
,
kwargs
=
{
'id'
:
self
.
request
.
user
.
id
})
ap_12/core/settings.py
View file @
0edc3494
...
...
@@ -113,7 +113,7 @@ TIME_ZONE = 'UTC'
USE_I18N
=
True
USE_L10N
=
Tru
e
USE_L10N
=
Fals
e
USE_TZ
=
True
...
...
@@ -142,3 +142,6 @@ AUTH_USER_MODEL = 'accounts.User'
MEDIA_ROOT
=
BASE_DIR
/
'uploads'
MEDIA_URL
=
'/images/'
# localhost:1025/uploads/avatars/user_1.jpg
# DATE_INPUT_FORMATS = ('%d.%m.%Y',)
ap_12/templates/auth/change_password.html
0 → 100644
View file @
0edc3494
{% extends 'base.html' %}
{% block content %}
<h1
class=
"text-center"
>
Смена пароля
</h1>
<form
action=
""
method=
"post"
>
{% csrf_token %}
{{ form.old_password.label }}
<input
type=
"password"
class=
"form-control mb-3"
name=
"old_password"
>
{% if form.old_password.errors %}
{% for error in form.old_password.errors %}
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% endif %}
{{ form.new_password1.label }}
<input
type=
"password"
class=
"form-control mb-3"
name=
"new_password1"
>
{% if form.new_password1.errors %}
{% for error in form.new_password1.errors %}
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% endif %}
{{ form.new_password2.label }}
<input
type=
"password"
class=
"form-control mb-3"
name=
"new_password2"
>
{% if form.new_password2.errors %}
{% for error in form.new_password2.errors %}
<div
class=
"alert alert-danger"
>
{{ error }}
</div>
{% endfor %}
{% endif %}
<button
class=
"btn btn-success"
>
Submit
</button>
</form>
{% endblock %}
\ No newline at end of file
ap_12/templates/auth/user_detail.html
View file @
0edc3494
...
...
@@ -14,6 +14,9 @@
<p>
Дата Рождения: {{ user_obj.profile.birth_date|date:'d.m.Y' }}
</p>
<p>
Почта: {{ user_obj.email }}
</p>
<a
href=
"{% url 'user_update' %}"
class=
"btn btn-warning mt-3"
>
Обновить
</a>
<a
href=
"{% url 'change_password' %}"
class=
"btn btn-primary mt-3"
>
Сменить пароль
</a>
<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 %}
...
...
ap_12/templates/auth/user_update.html
0 → 100644
View file @
0edc3494
{% extends 'base.html' %}
{% block content %}
<h1
class=
"text-center"
>
Изменить личные данные
</h1>
<form
action=
""
method=
"post"
enctype=
"multipart/form-data"
>
{% csrf_token %}
{% include 'partial/form.html' with fields_only=True %}
{% include 'partial/form.html' with form=profile_form fields_only=True %}
<button
class=
"btn btn-success mt-5"
>
Изменить
</button>
</form>
{% endblock %}
\ No newline at end of file
ap_12/uploads/avatars/capibara.jpg
0 → 100644
View file @
0edc3494
225 KB
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