سلام این حالت برای کاربری است که از قبل وارد سیستم شده و حالا می خواهد پسورد خود را عوض کند. برای این کار به سه فیلد احتیاج است. پسورد قبلی، پسورد جدید و تأیید پسورد جدید. خوب در گام اول URL را اضافه می کنیم. myproject/urls.py
۱ ۲ ۳ ۴ |
url(r'^settings/password/$', auth_views.PasswordChangeView.as_view(template_name='password_change.html'), name='password_change'), url(r'^settings/password/done/$', auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html'), name='password_change_done'), |
این فرم …
سلام ساخت فایل قالب: templates/password_reset_complete.html
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ |
{% extends 'base_accounts.html' %} {% block title %}Password changed!{% endblock %} {% block content %} <div class="row justify-content-center"> <div class="col-lg-6 col-md-8 col-sm-10"> <div class="card"> <div class="card-body"> <h3 class="card-title">Password changed!</h3> <div class="alert alert-success" role="alert"> You have successfully changed your password! You may now proceed to log in. </div> <a href="{% url 'login' %}" class="btn btn-secondary btn-block">Return to log in</a> </div> </div> </div> </div> {% endblock %} |
و حالا هم فایل تست: accounts/tests/test_view_password_reset.py
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ |
from django.contrib.auth import views as auth_views from django.core.urlresolvers import reverse from django.urls import resolve from django.test import TestCase class PasswordResetCompleteTests(TestCase): def setUp(self): url = reverse('password_reset_complete') self.response = self.client.get(url) def test_status_code(self): self.assertEquals(self.response.status_code, ۲۰۰) def test_view_function(self): view = resolve('/reset/complete/') self.assertEquals(view.func.view_class, auth_views.PasswordResetCompleteView) |
ترجمۀ اختصاصی توسط تمدن مطلب بعدی: تغییر پسورد مطلب قبلی: ویوی تأیید بازیابی پسورد
سلام نوشتن فایل قالب به شکل زیر: templates/password_reset_confirm.html
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ |
{% extends 'base_accounts.html' %} {% block title %} {% if validlink %} Change password for {{ form.user.username }} {% else %} Reset your password {% endif %} {% endblock %} {% block content %} <div class="row justify-content-center"> <div class="col-lg-6 col-md-8 col-sm-10"> <div class="card"> <div class="card-body"> {% if validlink %} <h3 class="card-title">Change password for @{{ form.user.username }}</h3> <form method="post" novalidate> {% csrf_token %} {% include 'includes/form.html' %} <button type="submit" class="btn btn-success btn-block">Change password</button> </form> {% else %} <h3 class="card-title">Reset your password</h3> <div class="alert alert-danger" role="alert"> It looks like you clicked on an invalid password reset link. Please try again. </div> <a href="{% url 'password_reset' %}" class="btn btn-secondary btn-block">Request a new password reset link</a> {% endif %} </div> </div> </div> </div> {% endblock %} |
این صفحه با استفاده از tokenای که در ایمیل ارسال شده قابل دسترسی است. (در فاز توسعۀ نرم افزار این توکن در کنسول قابل مشاهده است.) اگر لینک معتبر باشد صفحۀ زیر نمایش داده می شود. و اگر قبلاً از این …
سلام نوشتن فایل قالب به ترتیب زیر: templates/password_reset_done.html
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ |
{% extends 'base_accounts.html' %} {% block title %}Reset your password{% endblock %} {% block content %} <div class="row justify-content-center"> <div class="col-lg-4 col-md-6 col-sm-8"> <div class="card"> <div class="card-body"> <h3 class="card-title">Reset your password</h3> <p>Check your email for a link to reset your password. If it doesn't appear within a few minutes, check your spam folder.</p> <a href="{% url 'login' %}" class="btn btn-secondary btn-block">Return to log in</a> </div> </div> </div> </div> {% endblock %} |
و این هم از خروجی: و مثل همیشه نوشتن فایل تست: accounts/tests/test_view_password_reset.py
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ |
from django.contrib.auth import views as auth_views from django.core.urlresolvers import reverse from django.urls import resolve from django.test import TestCase class PasswordResetDoneTests(TestCase): def setUp(self): url = reverse('password_reset_done') self.response = self.client.get(url) def test_status_code(self): self.assertEquals(self.response.status_code, ۲۰۰) def test_view_function(self): view = resolve('/reset/done/') self.assertEquals(view.func.view_class, auth_views.PasswordResetDoneView) |
ترجمۀ اختصاصی توسط تمدن مطلب بعدی: ویوی تأیید بازیابی پسورد مطلب قبلی: ویوی بازیابی پسورد
سلام فایل زیر را ویرایش می کنیم. templates/password_reset.html
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ |
{% extends 'base_accounts.html' %} {% block title %}Reset your password{% endblock %} {% block content %} <div class="row justify-content-center"> <div class="col-lg-4 col-md-6 col-sm-8"> <div class="card"> <div class="card-body"> <h3 class="card-title">Reset your password</h3> <p>Enter your email address and we will send you a link to reset your password.</p> <form method="post" novalidate> {% csrf_token %} {% include 'includes/form.html' %} <button type="submit" class="btn btn-primary btn-block">Send password reset email</button> </form> </div> </div> </div> </div> {% endblock %} |
حالا نوشتن فایل تست. accounts/tests/test_view_password_reset.py
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ ۳۵ ۳۶ ۳۷ ۳۸ ۳۹ ۴۰ ۴۱ ۴۲ ۴۳ ۴۴ ۴۵ ۴۶ ۴۷ ۴۸ ۴۹ ۵۰ ۵۱ ۵۲ ۵۳ ۵۴ ۵۵ ۵۶ ۵۷ ۵۸ ۵۹ ۶۰ ۶۱ ۶۲ ۶۳ ۶۴ ۶۵ ۶۶ ۶۷ ۶۸ ۶۹ |
from django.contrib.auth import views as auth_views from django.contrib.auth.forms import PasswordResetForm from django.contrib.auth.models import User from django.core import mail from django.core.urlresolvers import reverse from django.urls import resolve from django.test import TestCase class PasswordResetTests(TestCase): def setUp(self): url = reverse('password_reset') self.response = self.client.get(url) def test_status_code(self): self.assertEquals(self.response.status_code, ۲۰۰) def test_view_function(self): view = resolve('/reset/') self.assertEquals(view.func.view_class, auth_views.PasswordResetView) def test_csrf(self): self.assertContains(self.response, 'csrfmiddlewaretoken') def test_contains_form(self): form = self.response.context.get('form') self.assertIsInstance(form, PasswordResetForm) def test_form_inputs(self): ''' The view must contain two inputs: csrf and email ''' self.assertContains(self.response, '<input', ۲) self.assertContains(self.response, 'type="email"', ۱) class SuccessfulPasswordResetTests(TestCase): def setUp(self): email = 'john@doe.com' User.objects.create_user(username='john', email=email, password='۱۲۳abcdef') url = reverse('password_reset') self.response = self.client.post(url, {'email': email}) def test_redirection(self): ''' A valid form submission should redirect the user to `password_reset_done` view ''' url = reverse('password_reset_done') self.assertRedirects(self.response, url) def test_send_password_reset_email(self): self.assertEqual(۱, len(mail.outbox)) class InvalidPasswordResetTests(TestCase): def setUp(self): url = reverse('password_reset') self.response = self.client.post(url, {'email': 'donotexist@email.com'}) def test_redirection(self): ''' Even invalid emails in the database should redirect the user to `password_reset_done` view ''' url = reverse('password_reset_done') self.assertRedirects(self.response, url) def test_no_reset_email_sent(self): self.assertEqual(۰, len(mail.outbox)) |
ساخت عنوان ایمیل ارسالی: templates/password_reset_subject.txt
۱ |
[Django Boards] Please reset your password |
ساخت متن ایمیل ارسالی: templates/password_reset_email.html
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ |
Hi there, Someone asked for a password reset for the email address {{ email }}. Follow the link below: {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} In case you forgot your Django Boards username: {{ user.username }} If clicking the link above doesn't work, please copy and paste the URL in a new browser window instead. If you've received this mail in error, it's likely that another user entered your email address by mistake while trying to reset a password. If you didn't initiate the request, you don't need to take any further action and can safely disregard this email. Thanks, The Django Boards Team |
و نهایتاً نمایش ایمیل به صورت کامل در کنسول برنامه یک فایل جداگانۀ تست باید ایجاد کنیم که فقط ارسال ایمیل را تست …
سلام بازیابی پسورد به چهار ویو احتیاج دارد. یک صفحه با یک فرم برای شروع پروسه یک صفحه که به کاربر نشان دهد بازیابی پسورد شروع شده و مثلاً لازم است اسپم ایمیل خود را هم چک کند. صفحه ای برای چک توکن ارسالشده توسط ایمیل صفحه ای که به …
سلام همانطور که گفته شد ایدۀ اصلی این ویژگی آن است که در زمان توسعۀ برنامه نیازی به ارسال ایمیل های واقعی نباشد. حالا دو حالت وجود دارد. یکی اینکه ایمیل را در یک فایل ذخیره کند و بعداً آن را چک کنیم یا اینکه ایمیل ارسالی را در کنسول …
سلام در خصوص بازیابی پسورد دو مسئله وجود دارد. یکی اینکه ساخت URL آن کمی پیچیده است که همانطور که در قبلاً گفته شد قرار نیست در regular expression متخصص باشیم و فعلاً می توانیم آن را کپی کنیم! دوم اینکه برای بازیابی پسورد به ارسال ایمیل احتیاج داریم که …
سلام اول از همه می خواهیم تست های مربوط به بورد را (مثل فایل های تست مربوط به برنامۀ اکانت) کمی خوشگل کنیم! فولدر تست را ایجاد می کنیم و درون آن فایل خالیِ init__.py__ قرار می دهیم. فایل tests.py را درون فایل جدید کپی می کنیم و نام آن …
سلام داخل فولدر برنامۀ boards یک فولدر با نام templatetags و درون آن دو فایل خالی با نامهای init__.py__ و form_tags.py ایجاد می کنیم.
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ |
myproject/ |-- myproject/ | |-- accounts/ | |-- boards/ | | |-- migrations/ | | |-- templatetags/ <-- here | | | |-- __init__.py | | | +-- form_tags.py | | |-- __init__.py | | |-- admin.py | | |-- apps.py | | |-- models.py | | |-- tests.py | | +-- views.py | |-- myproject/ | |-- static/ | |-- templates/ | |-- db.sqlite3 | +-- manage.py +-- venv/ |
می خواهیم دو عدد تگِ قالب درون فایل form_tags.py ایجاد کنیم. boards/templatetags/form_tags.py
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ |
from django import template register = template.Library() @register.filter def field_type(bound_field): return bound_field.field.widget.__class__.__name__ @register.filter def input_class(bound_field): css_class = '' if bound_field.form.is_bound: if bound_field.errors: css_class = 'is-invalid' elif field_type(bound_field) != 'PasswordInput': css_class = 'is-valid' return 'form-control {}'.format(css_class) |
(اگر معتبر بود و پسورد نبود کلاس css را مطابق با ‘is-valid’ …