没有验证的Django管理 [英] Django Admin without Authentication
问题描述
有没有任何形式的认证使用Django管理页面的准备方法?我知道我可以使用这种方法,但那是针对Django 1.3。有没有任何改变让我在Django 1.6中更容易做到这一点?
我的主要动机是我想尽可能少的数据库表,我只在本地使用,所以没有必要进行任何身份验证(我只在本地主机上运行服务器)。
创建一个模块 auto_auth.py
:
来自django.contrib.auth.models import User
class Middleware(object):
def process_request(self,request):
request.user = User.objects.filter )[0]
编辑 MIDDLEWARE_CLASSES
settings.py
:
- 删除
'django.contrib .auth.middleware.AuthenticationMiddleware'
- 添加
'auto_auth.Middleware'
您可以将 User.objects.filter()[0]
更改为其他内容如果你想要一个特定的用户。
回应你的评论:是的。尝试这样:
类用户:
/ pre>
is_superuser = True
is_active = True
is_staff = True
id = 1
def return_true(* args,** kwargs):
return True
User.has_module_perms = return_true
User.has_perm = return_true
class中间件(对象):
def process_request(self,request):
request.user = User()
并从
INSTALLED_APPS $中删除
'django.contrib.auth'
c $ c>
但是,如果您使用任何依赖于auth应用程序的应用程序,您将有一个糟糕的时间。
Is there a ready way to use the Django admin page without any form of authentication? I know I can use this method, but that was for Django 1.3. Are there any changes that would let me do this more easily in Django 1.6?
My main motivation for this is that I want to have as few database tables as possible, and I am using this only locally, so there is no need for any sort of authentication (I'm only ever running the server on localhost anyways).
解决方案Create a module
auto_auth.py
:from django.contrib.auth.models import User class Middleware(object): def process_request(self, request): request.user = User.objects.filter()[0]
Edit
MIDDLEWARE_CLASSES
in yoursettings.py
:
- Remove
'django.contrib.auth.middleware.AuthenticationMiddleware'
- Add
'auto_auth.Middleware'
You can change User.objects.filter()[0]
to something else if you want a particular user.
In response to your comment: yes. Try this:
class User:
is_superuser = True
is_active = True
is_staff = True
id = 1
def return_true(*args, **kwargs):
return True
User.has_module_perms = return_true
User.has_perm = return_true
class Middleware(object):
def process_request(self, request):
request.user = User()
And remove 'django.contrib.auth'
from INSTALLED_APPS
But if you use any apps that depend on the auth app, you're going to have a bad time.
这篇关于没有验证的Django管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!