我正在试图弄清楚Django Groups,并且该网站上的文档非常简单.
例如,您可以使用装饰器permission_required()来检查权限,但是,这仅检查您是否直接分配了权限.我已将用户分配给具有权限设置的组.使用Django的权限系统时,它会忽略用户所属的组.
有没有办法让Django继承用户组的权限?
由于用户身份验证,我的应用中的错误已经丢失了一些时间.我认为这有点令人困惑,但也许有人可以解释原因,它在我看来非常符合逻辑.
的user.is_staff是一个成员变量,而user.is_authenticated是一种方法.但是is_authenticated只有返回true或false取决于如果该类User或AnonymousUser(见http://docs.djangoproject.com/en/dev/topics/auth/)
这有什么理由吗?为什么user.is_authenticated是方法?
提前致谢
Django的noob问题:
我使用dango.contrib.auth来管理我的网站用户.但是现在,我正在开发"设置页面",用户可以在其中编辑他的名字,姓氏和电子邮件地址.但在设置页面中我还想要一个"简报"复选框.
问题是:1)我应该在哪里将新闻通讯字段放在数据库中?2)如何创建用于编辑这些信息的表单?
谢谢.
- 更新 -
现在我在models.py中这个:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True)
favourite_color = models.CharField(max_length = 40)
Run Code Online (Sandbox Code Playgroud)
这在forms.py中:
class UserSettingsForm(forms.ModelForm):
class Meta:
model = User
exclude = ('password',)
def save(self, commit=False):
user = super(UserSettingsForm,self).save(commit)
favourite_color = self.cleaned_data.get('favourite_color', '')
if favourite_color and user.favourite_color is None:
UserProfile(user=user).save()
if not slug:
UserProfile.objects.filter(user=user).delete()
if not commit:
user.save()
return user
Run Code Online (Sandbox Code Playgroud)
我有点困惑.我会在设置表单中编辑名字,姓氏,电子邮件和喜欢的颜色等信息,但实际上我做错了.
嗨,我正在尝试使用这里提到的user_passes_test装饰器.但我一直收到这个错误:
'bool' object is not callable
Run Code Online (Sandbox Code Playgroud)
我的用法:
@user_passes_test(lambda u: u.is_active() and u.is_staff())
def fulfillment(request):
...
Run Code Online (Sandbox Code Playgroud) 我有几个通知可以从我的Django申请发送出去users.这些用户通过django.contrib.auth应用程序进行管理.
我需要跟踪每个用户是否已指定他/她想要获得该通知.
我应该在哪里保存这些信息?
我打算创建自己的自定义表,但我看到也许我可以在auth模块中的某处保存这些信息?
Django和Tastypie有基本的身份验证示例吗?我对Django中的身份验证是如何工作有点困惑,特别是对于Tastypie.我想知道身份验证如何与api密钥一起工作以及如何使用Django内置的User模型对用户进行身份验证.任何建议或代码都非常感谢.谢谢.
我需要有两个不同的登录/注册系统.
1: One for the general user.
2: Second for the Channels admin.
Both will have different email id, password etc in two different tables.
For the general user it will go in the `auth_user` table and for the channel
it I'd be creating another different models/table.
Run Code Online (Sandbox Code Playgroud)
我知道django提供了一个完整的身份验证系统,我可以为普通用户使用.但是如何同时在渠道管理员的情况下实现相同的功能呢?
我试着查看django的文档中的AUTHENTICATION_BACKENDS和AUTH_USER_MODEL,
我无法理解如何为频道管理部分设置会话.
所以,如果有人能够让我知道如何以及如何同时实现这两者的方法.
我通过添加自定义"配置文件"模型,然后在用户save/create上实例化,使用1.4x方法扩展了用户对象.在我的注册过程中,我想向配置文件模型添加其他信息.视图成功呈现,但配置文件模型不保存.代码如下:
user = User.objects.create_user(request.POST['username'], request.POST['email'], request.POST['password'])
user.save()
profile = user.get_profile()
profile.title = request.POST['title']
profile.birthday = request.POST['birthday']
profile.save()
Run Code Online (Sandbox Code Playgroud) python django django-models django-views django-authentication
当我尝试使用视图的URL时@login_required,它会将我重定向到登录表单.但渲染的表单没有action=属性,当我点击提交按钮时没有任何反应.但是当我去的时候,/admin/我得到了工作登录表单.以这种方式登录后,所有视图都正常工作.我正在使用admin_bootstrap它,这是不同的登录表单.但为什么默认的那种方式呢?
相关代码:
mySubApp/views.py:
@login_required
def index(request):
user = request.user
return HttpResponse("Hello, world. You're at index.")
Run Code Online (Sandbox Code Playgroud)
mainApp/urls.py:
#login
url(r'^login/$', 'django.contrib.auth.views.login', name='LogMeIn'),
#mySubApp
url(r'^subapp/', include('mySubApp.urls')),
Run Code Online (Sandbox Code Playgroud)
mySubApp/urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
Run Code Online (Sandbox Code Playgroud)
settings.py:
LOGIN_URL = 'LogMeIn'
Run Code Online (Sandbox Code Playgroud)
我是否需要创建自定义登录表单才能使其正常工作?
我使用UserCreationForm来创建新用户.
from django.contrib.auth.forms import UserCreationForm
class RegistrationForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'is_active']
Run Code Online (Sandbox Code Playgroud)
UserCreationForm自动添加两个字段(Password1和Password2).如果密码太短,则会引发错误,告诉它.或者,如果它太简单或常见.这是通过django.contrib.auth.password_validation.
我想知道我是否可以覆盖这些错误的消息.
现在密码验证的源代码是:
def validate(self, password, user=None):
if len(password) < self.min_length:
raise ValidationError(
ungettext(
"This password is too short. It must contain at least %(min_length)d character.",
"This password is too short. It must contain at least %(min_length)d characters.",
self.min_length
),
code='password_too_short',
params={'min_length': self.min_length},
)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在表单定义中使用此代码来覆盖此错误消息时,标签会更改,但error_messages保持不变:
password1 = forms.CharField(label='New Label', error_messages={'password_too_short': 'My error message for …Run Code Online (Sandbox Code Playgroud)