django customize reset password form - python

I am a beginner in django (django 1.7 python 2.7).
I am trying to add no captcha recaptcha onto my django reset password form.
I am trying to use this recaptcha djano plugin.
I have followed the instructions and added the necessay settings:
Installed django-recaptcha to the Python path.
Added captcha to the INSTALLED_APPS setting.
Added the following to my settings.py file:
RECAPTCHA_PUBLIC_KEY = '76wtgdfsjhsydt7r5FFGFhgsdfytd656sad75fgh' # fake - for the purpose of this post.
RECAPTCHA_PRIVATE_KEY = '98dfg6df7g56df6gdfgdfg65JHJH656565GFGFGs' # fake - for the purpose of this post.
NOCAPTCHA = True
The instructions then advise to add the captcha to the form, like so:
from django import forms
from captcha.fields import ReCaptchaField
class FormWithCaptcha(forms.Form):
captcha = ReCaptchaField()
How do I access the built in reset password form? As a beginner, I suspect that I have to customise the built in reset password form, but how do I do that? I am not even sure where the built in reset password form is. An example of how to customise the build in reset password form or a push to a tutorial would be handy.
I have searched SO & google, but could not find anything suitable.

You want to customise the PasswordReset view. By default, it uses the PasswordResetForm, which you can customize.
# in e.g. myapp/forms.py
from django.contrib.auth.forms import PasswordResetForm
class CaptchaPasswordResetForm(PasswordResetForm):
captcha = ReCaptchaField()
...
Then in your urls.py, import your form, and use the form_class to specify the form.
from django.contrib.auth import views as auth_views
from django.urls import path
from web.forms import CaptchaPasswordResetForm
urlpatterns = [
path("accounts/password_reset/", auth_views.PasswordResetView.as_view(form_class=CaptchaPasswordResetForm)),
]
For Django < 1.11, you need to customise the URL pattern for the password_reset view, and set password_reset_form to
from django.contrib.auth import views as auth_views
from myapp.forms import CaptchaPasswordResetForm
urlpatterns = [
...
url(
r'^password_reset/',
auth_views.password_reset,
{'password_reset_form': CaptchaPasswordResetForm},
)
]
For more information about including password reset views in your urls, see the docs.

Related

Facing troubles on bringing the Django admin user tools like welcome, view site, log out on the custom template

I have to override the blocks like branding,site_title, and index title to a custom template. But the user tools are not displaying.
How I get.
How I want.
You can update the Site Header from your main urls.py. Just add in:
from django.contrib import admin
....
admin.site.site_header = "the title you want"
If you want to customise the admin site alot, you will likely want to subclass/override the default admin site. The documentation has most the info you need.
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#customizing-the-adminsite-class
If you’d like to set up your own admin site with custom behavior, you’re free to subclass AdminSite and override or add anything you like. Then, create an instance of your AdminSite subclass (the same way you’d instantiate any other Python class) and register your models and ModelAdmin subclasses with it instead of with the default site. Finally, update myproject/urls.py to reference your AdminSite subclass.
app/admin.py
from django.contrib.admin import AdminSite
from .models import MyModel
class MyAdminSite(AdminSite):
site_header = 'Monty Python administration'
admin_site = MyAdminSite(name='myadmin')
admin_site.register(MyModel)
project/urls.py
from django.urls import path
from myapp.admin import admin_site
urlpatterns = [
path('myadmin/', admin_site.urls),
]

How to accept data in ModelForm and display data from DB in same webpage

I want to have a ModelForm that accepts user input and on the same webpage, also display all the records in the database. I created one view with a ModelForm called views.users and another view that displays all the users from the database called views.display_users. How do I put these both in my urls.py file? My urls.py code is below. Thank you!
# urls.py
from django.contrib import admin
from django.urls import path
from app3 import views
urlpatterns = [
path('users/',views.users,name='users'),
path('users/',views.display_users,name='display_users'),
]
You can integrate django-restframework link. This is one of the most popular django third party app to use in project. Django restframework provide viewset, where you can retrive, post, put, delete, list and also add many custom action in same end-point. Viewset tutorial link.

How to use different view for django-registration?

I have been trying to get django-registration to use the view RegistrationFormUniqueEmail and following the solution from this django-registration question. I have set my urls.py to
from django.conf.urls import patterns, include, url
from registration.forms import RegistrationFormUniqueEmail
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^users/', include('registration.backends.default.urls')),
url(r'^users/register/$', 'registration.backends.default.views.RegistrationView',
{'form_class': RegistrationFormUniqueEmail,
'backend': 'registration.backends.default.DefaultBackend'},
name='registration_register'),
)
However, I can still create multiple accounts with the same email. What is the problem? Shouldn't django-registration be using the view that I specified? I am currently using django-registration 0.9b1.
The version of Django registration you are using has been rewritten to use class based views. This means a different approach is required in your urls.py.
First, You need to subclass the RegistrationView, and set the custom form class.
from registration.backends.default.views import RegistrationView
from registration.forms import RegistrationFormUniqueEmail
class RegistrationViewUniqueEmail(RegistrationView):
form_class = RegistrationFormUniqueEmail
Then, use your custom RegistrationViewUniqueEmail subclass in your urls. As with other class based views, you must call as_view().
url(r'^user/register/$', RegistrationViewUniqueEmail.as_view(),
name='registration_register'),
Make sure your customised registration_register view comes before you include the default registration urls, otherwise it won't be used.
The version 1.2 of django-registration-redux allows the unique email option with the following urls.py patterns:
url(r'^accounts/register/$', RegistrationView.as_view(form_class=RegistrationFormUniqueEmail), name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls')),
If you need to do something more, like a specific URL option, you can subclass the RegistrationView in your app views.py and RegistrationForm in your app forms.py

Django-registration: how to include a custom backend?

I created a custom backend to have activation emails in html without editing django-registration code
(ref. django+ send email in html with django-registration, last answer)
this is the backend, stored in core/registration/backends/htmldefault/__init__.py, like DefaultBackend that is stored in registration/backends/__init__.py
from registration.forms import RegistrationForm
from core.models import HtmlRegistrationProfile
class HtmlDefaultBackend(object):
#etc same as DefaultBackend but using HtmlRegistrationProfile instead of RegistrationProfile
and this is my urls.py
urlpatterns = patterns('',
#...
url(r'^accounts/register/$', register, {'backend': 'core.registration.backends.htmldefault.HtmlDefaultBackend','form_class': UserRegistrationForm}, name='registration_register'),
url(r'^accounts/', include('registration.urls')),
#...
)
But i get
ImproperlyConfigured at /accounts/register/
Error loading registration backend core.registration.backends.htmldefault: "No module named registration.backends.htmldefault"
thrown by /registration/backends/__init__.py in get_backend, line 27
What i'm asking is.. is it possible to have a custom django-registration backend outside the registration package? Or it must live under /registration/backends/ like simple and default backends?
It is possible.
Check if all your folders (core, registration, backends,
htmldefault) have __init__.py (can be empty).
Core folder is in project directory?

DJANGO: How to allow Users to change password?

So I have Users (from django.contrib.auth.models import User) and UserProfiles. in my UserProfile view there is an edit link. This edit link allows a User to change their User settings. In the password section of the form I see help text that says:
"Use '[algo]$[salt]$[hexdigest]' or use the change password form."
The "change password form" is actually a link to http://127.0.0.1:8000/user/1/user_edit/password/, when I click the link I get an error message saying:
ViewDoesNotExist at /user/1/user_edit/password/
Could not import testdb.views.django.contrib.auth.views. Error was: No module named django.contrib.auth.views
I've been following the documentation: https://docs.djangoproject.com/en/dev/topics/auth/
What am I doing wrong? I hear that this should use djangos templates, do I need to copy those over to my apps template folder? if so, where are they?
URLS.PY
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('testdb.views',
url(r'^$', 'index'),
url(r'^^user/(?P<user_id>\d+)/$', 'user_detail'),
url(r'^user/(?P<user_id>\d+)/user_edit/$', 'user_edit'),
url(r'^user/(?P<user_id>\d+)/user_edit/password/$', 'django.contrib.auth.views.password_change', {'template_name': 'password_change_form'}),
)
You have a wrong URL pattern defined: Django tries to find testdb.views.django.contrib.auth.views as you define the password_change view inside patterns('testdb.views',.
Add a second pattern:
urlpatterns += patterns('django.contrib.auth.views',
url(r'^user/(?P<user_id>\d+)/user_edit/password/$', 'password_change')
)
That should resolve your issue.
cfedermann has a solution to your issue, but I'm confused as to why you've defined the password_change URL in the first place. This functionality is built-in to the admin, and - like all the other admin pages - the URL is defined already by the admin code itself.

Categories