在之前的项目中(使用Django 1.5),我使用了一个自定义的Django管理站点来为非员工用户提供对有限模型子集的访问。我使用了here提供的出色解释。这就像一个魅力。
对于一个新项目,我正在使用Django 1.7,并且我已经了解到不推荐使用check_for_test_cookie()。但是,只是评论它似乎并没有提供所需的功能。当我尝试登录我的user_admin网站时,我收到了一个错误,包括员工和非员工帐户。我收到的错误消息“请更正下面的错误”并没有给我任何额外的线索。
有关如何在Django 1.7中使用此功能的任何建议?我的代码如下。
的myapp / user_admin.py
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.contrib.admin.sites import AdminSite
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import ugettext_lazy
from django.utils.translation import ugettext as _
ERROR_MESSAGE = ugettext_lazy("Please enter the correct %(username)s and password "
"for a staff account. Note that both fields may be case-sensitive.")
class UserAdminAuthenticationForm(AuthenticationForm):
"""
Same as Django's AdminAuthenticationForm but allows to login
any user who is not staff.
"""
this_is_the_login_form = forms.BooleanField(widget=forms.HiddenInput,
initial=1,
error_messages={'required': ugettext_lazy(
"Please log in again, because your session has"
" expired.")})
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
message = ERROR_MESSAGE
if username and password:
self.user_cache = authenticate(username=username,
password=password)
if self.user_cache is None:
if u'@' in username:
# Mistakenly entered e-mail address instead of username?
# Look it up.
try:
user = User.objects.get(email=username)
except (User.DoesNotExist, User.MultipleObjectsReturned):
# Nothing to do here, moving along.
pass
else:
if user.check_password(password):
message = _("Your e-mail address is not your "
"username."
" Try '%s' instead.") % user.username
raise forms.ValidationError(message)
# Removed check for is_staff here!
elif not self.user_cache.is_active:
raise forms.ValidationError(message)
#self.check_for_test_cookie()
return self.cleaned_data
class UserAdmin(AdminSite):
login_form = UserAdminAuthenticationForm
def has_permission(self, request):
"""
Removed check for is_staff.
"""
return request.user.is_active
user_admin_site = UserAdmin(name='usersadmin')
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from myapp.user_admin import user_admin_site
from mypapp.models import *
from myapp.admin import *
admin.autodiscover()
admin.site.register(mystaffmodel,mystaffmodel_admin)
user_admin_site.register(mynonstaffmodel,mynonstaffmodel_admin)
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(user_admin_site.urls)),
)
答案 0 :(得分:0)
您的表格必须如下
#homepage .artists > table > tr > td > a > img{
max-width: 100%;
max-height: 100%;
}
这足以使您的代码正常工作。 : - )