mirror of
https://github.com/jazzband/django-admin2.git
synced 2026-04-05 07:21:05 +00:00
Hardcoded LoginView (#436)
* Add a way to customize LoginView and Document about it Resolve #416 * Add test for custom login view * Remove unused code in test_views
This commit is contained in:
parent
fdcdf5a484
commit
8d41a222b7
7 changed files with 103 additions and 7 deletions
|
|
@ -27,6 +27,7 @@ class Admin2(object):
|
|||
admin site.
|
||||
"""
|
||||
index_view = views.IndexView
|
||||
login_view = views.LoginView
|
||||
app_index_view = views.AppIndexView
|
||||
api_index_view = apiviews.IndexAPIView
|
||||
|
||||
|
|
@ -142,6 +143,7 @@ class Admin2(object):
|
|||
'registry': self.registry,
|
||||
'app_verbose_names': self.app_verbose_names,
|
||||
'apps': self.apps,
|
||||
'login_view': self.login_view,
|
||||
}
|
||||
|
||||
def get_app_index_kwargs(self):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
{% extends "djadmin2theme_bootstrap3/base.html" %}
|
||||
{% load i18n staticfiles admin2_tags %}
|
||||
|
||||
{% block navbar %}{% endblock navbar %}
|
||||
{% block breacrumbs %}{% endblock breacrumbs %}
|
||||
|
||||
{% block page_header %}{% endblock page_header %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-4 col-md-offset-4">
|
||||
<div class="login-panel panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Custom login view</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form method="post" class="">
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<input type="hidden" name="this_is_the_login_form" value="1"/>
|
||||
<input type="hidden" name="next" value="{{ next }}"/>
|
||||
<button class="btn btn-lg btn-success btn-block" type="submit">
|
||||
{% trans "Log in" %}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
from django.test import TestCase
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from .. import views
|
||||
|
||||
|
|
@ -16,3 +18,11 @@ class AdminViewTest(TestCase):
|
|||
|
||||
def test_name(self):
|
||||
self.assertEquals(self.admin_view.name, 'admin-view')
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='djadmin2.tests.urls')
|
||||
class CustomLoginViewTest(TestCase):
|
||||
|
||||
def test_view_ok(self):
|
||||
response = self.client.get(reverse("admin2:dashboard"))
|
||||
self.assertInHTML('<h3 class="panel-title">Custom login view</h3>', force_text(response.content))
|
||||
|
|
|
|||
20
djadmin2/tests/urls.py
Normal file
20
djadmin2/tests/urls.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.conf.urls import include, url
|
||||
from django.conf.urls.static import static
|
||||
|
||||
from djadmin2.site import djadmin2_site
|
||||
|
||||
from djadmin2.views import LoginView
|
||||
|
||||
|
||||
class CustomLoginView(LoginView):
|
||||
default_template_name = "custom_login_template.html"
|
||||
|
||||
djadmin2_site.login_view = CustomLoginView
|
||||
djadmin2_site.autodiscover()
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^admin2/', include(djadmin2_site.urls)),
|
||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
|
@ -76,6 +76,7 @@ class Admin2Mixin(PermissionMixin):
|
|||
model_admin = None
|
||||
model_name = None
|
||||
app_label = None
|
||||
login_view = None
|
||||
|
||||
index_path = reverse_lazy('admin2:dashboard')
|
||||
|
||||
|
|
@ -101,8 +102,6 @@ class Admin2Mixin(PermissionMixin):
|
|||
def dispatch(self, request, *args, **kwargs):
|
||||
|
||||
if self.is_user(request):
|
||||
from .views import LoginView
|
||||
|
||||
if request.path == reverse('admin2:logout'):
|
||||
return HttpResponseRedirect(self.index_path)
|
||||
|
||||
|
|
@ -110,8 +109,7 @@ class Admin2Mixin(PermissionMixin):
|
|||
extra = {
|
||||
'next': request.GET.get('next', self.index_path)
|
||||
}
|
||||
return LoginView().dispatch(request, extra_context=extra,
|
||||
*args, **kwargs)
|
||||
return self.login_view().dispatch(request, extra_context=extra, *args, **kwargs)
|
||||
|
||||
return super(Admin2Mixin, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
|
|
|
|||
|
|
@ -44,4 +44,39 @@ In your Django project's root URLconf module (``urls.py``) modify the code to in
|
|||
|
||||
In real projects the new IndexView would likely be placed into a ``views.py`` module.
|
||||
|
||||
.. note:: Considering that dashboard is more intuitive of a name, perhaps the ``IndexView`` should be renamed ``DashboardView``?
|
||||
.. note:: Considering that dashboard is more intuitive of a name, perhaps the ``IndexView`` should be renamed ``DashboardView``?
|
||||
|
||||
Customizing the Login view
|
||||
==========================
|
||||
|
||||
The login view could also be customized.
|
||||
|
||||
In your Django project's root URLconf module (``urls.py``) modify the code to include the commented code before the ``djadmin2.default.autodiscover()``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from django.conf.urls import patterns, include, url
|
||||
|
||||
from djadmin2.site import djadmin2_site
|
||||
from djadmin2.views import LoginView
|
||||
|
||||
|
||||
######### Begin django-admin2 customization code
|
||||
# Create a new django-admin2 index view
|
||||
class CustomLoginView(LoginView):
|
||||
|
||||
# specify the template
|
||||
default_template_name = "custom_login_template.html"
|
||||
|
||||
# override the default index_view
|
||||
djadmin2_site.login_view = CustomLoginView
|
||||
######### end django-admin2 customization code
|
||||
|
||||
djadmin2_site.autodiscover()
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^admin2/', include(djadmin2_site.urls)),
|
||||
# ... Place the rest of the project URLs here
|
||||
)
|
||||
|
||||
In real projects the new LoginView would likely be placed into a ``views.py`` module.
|
||||
|
|
|
|||
|
|
@ -18,4 +18,3 @@ urlpatterns = [
|
|||
url(r'^blog/detail(?P<pk>\d+)/$', BlogDetailView.as_view(template_name="blog/blog_detail.html"), name='blog_detail'),
|
||||
url(r'^$', BlogListView.as_view(template_name="blog/home.html"), name='home'),
|
||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue