Adding forgot-password feature to Django admin site - django

How to add the forgot-password feature to Django admin site? With email/security question options? Is there any plug-in/extension available?

They are all there built in the django. Just add the relevant url patterns. As follows.
from django.contrib.auth import views as auth_views
patterns+=('',
url(r'^passreset/$',auth_views.password_reset,name='forgot_password1'),
url(r'^passresetdone/$',auth_views.password_reset_done,name='forgot_password2'),
url(r'^passresetconfirm/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$',auth_views.password_reset_confirm,name='forgot_password3'),
url(r'^passresetcomplete/$',auth_views.password_reset_complete,name='forgot_password4'),
)
And, oh, while you are at it, also add the views and url patterns for password change.
url(r'^password/change/$',
auth_views.password_change,
name='auth_password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='auth_password_change_done'),
They are listed in the documentation of course.
You'll have to provide your own templates.

Actually since Django 1.4 there's an easy way to get the forgotten password link appear directly in the admin login page (which sounds like the precise question asked):
https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#adding-a-password-reset-feature
You can add a password-reset feature to the admin site by adding a few
lines to your URLconf. Specifically, add these four patterns:
url(r'^admin/password_reset/$',
'django.contrib.auth.views.password_reset',
name='admin_password_reset'), (r'^admin/password_reset/done/$',
'django.contrib.auth.views.password_reset_done'),
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$',
'django.contrib.auth.views.password_reset_complete'),
(This assumes
you’ve added the admin at admin/ and requires that you put the URLs
starting with ^admin/ before the line that includes the admin app
itself).
Changed in Django 1.4 The presence of the admin_password_reset named
URL will cause a “forgotten your password?” link to appear on the
default admin log-in page under the password box

Related

How to reverse match multiple Django admin sites (Custom admin site namespaces)

When you extend AdminSite to create another admin site how do you go about being able to reverse match each site? It seems the admin namespace is hardcoded reverse('admin:index'), is there a way to supply a custom namespace?
You may be confused with the namespace in django. If you are interested to clarify that confusion, you may read up the discussion here.
If you want to try solve your problem, there is a specific documentation for multiple admin sites.
Below are example solutions mostly copied from official documentation
Example Solution:
# urls.py
from django.conf.urls import url
from .sites import basic_site, advanced_site
urlpatterns = [
url(r'^basic-admin/', basic_site.urls),
url(r'^advanced-admin/', advanced_site.urls),
]
And
# sites.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
site_header = 'Monty Python administration'
basic_site = MyAdminSite(name='myadminbasic')
advanced_site = MyAdminSite(name='myadminadvanced')
Reversing
reverse('myadminbasic:index') # /basic-admin/
reverse('myadminadvanced:index') # /advanced-admin/

combining django-authtools and django-allauth in a Django project

I am having a Django (1.8) project with allauth integrated. Now I would like to have a custom user model instead of the auth.user one (which I am aware is configurable, but I need more flexibility). When I follow the django-authtools installation instruction one step is to add following line to the url-patterns, see
https://django-authtools.readthedocs.org/en/latest/intro.html#installation
url(r'^accounts/', include('authtools.urls')),
However this URL pattern is already taken by the allauth app (in line the instructions thereof, see
http://django-allauth.readthedocs.org/en/latest/installation.html
):
url(r'^accounts/', include('allauth.urls')),
So how should I proceed?
It doesn't really matter on what URL will you include that additional patterns, you can change URL both for allauth and authtools, because that URL is not hardcoded anywhere, it will be reversed automatically by those apps when needed. So you can for example use:
url(r'^auth/', include('authtools.urls')),
url(r'^accounts/', include('allauth.urls')),
You can also include both pattern groups on one url prefix, but if there is conflict (for example both allauth and authtools registers `accounts/login/' URL), django will serve view for first entry in urlconf.

Change header 'Django administration' text on nginx

I followed this question's answers to change my django admin panel title header.
I tried this:
There is an easy way to set admin site header - assign it to current
admin instance in urls.py like this
admin.site.site_header = 'My admin'
But it just works when I'm running the page via Python manage.py runserver
My question is how can I change the admin title header when I'm running the site via gunicorn and nginx
writing this code at the bottom of urls.py somehow worked:
admin.site.site_header = 'My admin'
If you already have an admin.py file started wherein you have registered your particular models, you can simply adjust these values there.
your_app/admin.py
# Simple admin setup
from django.contrib import admin
from .models import MyModel
# Register model
admin.site.register(MyModel)
# Tweak admin site settings like title, header, 'View Site' URL, etc
admin.site.site_title = 'My App Admin'
admin.site.site_header = 'My App Admin'
You can find all the attributes here.
follow the below steps to customize the site header and site title text of django admin login page :
1.)First import admin module in settings.py file like as below :
from django.contrib import admin
2.)In bottom of settings.py file add these lines:
admin.site.site_header = 'MY_SITE_HEADER'
admin.site.site_title = 'MY_SITE_TITLE'
The above method works in latest version of django i.e.1.11.3 till date.
You can make changes to parts of the admin by providing a template in an admin subdir of your templates directory to override what is provided by admin.
In this case, you'd want to provide a base_site.html template. You can see what the default one looks like here: https://github.com/django/django/blob/master/django/contrib/admin/templates/admin/base_site.html

Django Registration with Django cms

I have integrated django-registration with django-cms. I have multilingual django-cms for my site with two languages English and french. I am facing issue in url-mapping. as django-cms has multiple language, so it attached language code after domain name. While in django-registration it consider such url as 404.
Below is flow, I putted registration button on click of that I have explicitly set to http://localhost:8000/accounts/register/ and it display registration page properly, but after successful completion of registration, it redirects to http://localhost:8000/en/accounts/register/complete/ , where language code is attached with url and django-registration says page not found. If I manually remove language code from url, it works fine.
Can anybody help me ?
url.py for project.
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls'), name="home"),
url(r'^news/', include('multilingual_news.urls')),
url(r'^search/', include('haystack.urls')),
url(r'^member/',include('openerp_member.urls')),
(r'^accounts/',include('registration.backends.default.urls')),
)
You need to add an AppHook for Django-Registration. So what I did was:
create a new app called "cmsauth"
create the according cms_apps.py:
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
class RegistrationApphook(CMSApp):
name = _("RegistrationApphook")
urls = ["registration.backends.hmac.urls"]
apphook_pool.register(RegistrationApphook)
create a CMS Page like "Accounts"
link the AppHook RegistrationApphook (you might need to restart server for it to become available)
Now you should be able to reach the URLs.

Django: Views problem with Django + django-registration + jinja

So, I have a django project that is using jinja2 rendering, and I also installed django-registration to make my life easier. I ran into the following problem:
Going to homepage I render it with jinja. In order to check for authentication, I have to use jinja's syntax, which is user.is_authenticated(). However, in regular django templating, this check is done with user.is_authenticated. If in regular django templating there are (), it gives error.
So going to the /accounts/login/ page, the django-registration modul doesn't do anything special, so it forwards the url to the standard django views the following way:
from django.contrib.auth import views as auth_views
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html'},
name='auth_login'),
So I know for sure I shouldn't be changing the django.contrib.auth view, but then where do i put my own view? In myapp/views.py?
And also, do I have to copy paste the django view, and then modify on top of it (in this case simply replace the render with render_jinja) or is there a way to 'extend' this original django view to my own slightly modified view for logging in?
Whether right or wrong, in the registration module, I made a new view, that handled the logging, copying a few lines from here and there. It logical and seems to be working fine.