在Django中(当使用django.contrib.auth时)我可以添加Group到另一个Group吗?即,Group是另一个成员Group?
如果是这样,我该怎么做?我将Users 添加到一个Group使用user_set,但从我收集的默认Group模型没有多少到它自己.
文档:https://docs.djangoproject.com/en/1.7/topics/auth/default/#groups
在Django项目中,我有一个自定义用户模型,它添加了一个额外的字段:
class User(AbstractUser):
company = models.ForeignKey(Company, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
比如说,这个模型在我的应用程序中定义MyApp.models.
如何让新User模型在"身份验证和授权"下显示为原始django.contrib.auth模型?
我去了我的网页http://localhost:8000/listings/post/,它没有通过测试
@user_passes_test(lambda u: u.is_authenticated() and u.get_profile().shipper)
Run Code Online (Sandbox Code Playgroud)
正如所料,并重定向我http://localhost:8000/login/?next=/listings/post/喜欢它应该,但当我再次登录时,它不会像我想象的那样将我重定向回那个页面.它需要我/accounts/profile/.我没有redirect_field_name在任何地方定义,所以它应该寻找默认next变量.相关urls.py位看起来像这样
url(r'^login/$', 'django.contrib.auth.views.login', name='login'),
Run Code Online (Sandbox Code Playgroud)
那么可能的原因是什么?
我在Facebook上验证会话后写了一个登录用户的facebook-connect应用程序,问题是如何在获取用户对象后在django上验证用户?
user = User.objects.get(email=email)
user = authenticate(username=user.username, password=user.password)
login(request, user)
Run Code Online (Sandbox Code Playgroud)
还有另一种方法来实现这一目标吗?
get_absolute_url()方法很酷,但在某些情况下不需要.django.contrib.auth.models.User默认设置它,这导致我的项目在管理员中有一个断开的链接.
我怎样才能防止这种情况发生?
在我的一个旧项目中,我设置了一个自定义模板,其中我删除了按钮的html,它听起来不像是一个可扩展的好解决方案.还有什么更好的?
我正在使用Django密码重置.
我在settings.py中有这个代码:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myusername@gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
DEFAULT_FROM_EMAIL = 'myusername@gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SERVER_EMAIL = 'myusername@gmail.com'
Run Code Online (Sandbox Code Playgroud)
它将我重定向到正确的页面,但它不会发送电子邮件.我检查了垃圾邮件文件夹等,但仍然没有:(
任何想法都非常感谢!
编辑
我试图使用控制台测试它,但我收到以下错误:
>>> email = EmailMessage('Mail test', 'this is a test', to=['myusername@gmail.com'])
>>> email.send()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 88, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 55, in open
self.connection.login(self.username, self.password) …Run Code Online (Sandbox Code Playgroud) 在settings.py中导入自定义后端的正确方法是什么?我目前在settings.py中有以下内容:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
Run Code Online (Sandbox Code Playgroud)
其中apployment_site是应用程序,auth是文件名,CustomAuth是类名.
在我看来,我得到:ImportError: a doesn't look like a module path在我运行以下代码之后:
from django.contrib.auth import authenticate
from apployment_site import *
authenticate(username="username", password="password")
Run Code Online (Sandbox Code Playgroud)
这是我的完整settings.py:
"""Django settings for apployment project.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: …Run Code Online (Sandbox Code Playgroud) 我正在尝试让django密码重置工作,但重置电子邮件不会被发送.
我知道我的电子邮件配置正确,因为以下内容适用于shell和我的一个视图(我用它来获取支持电子邮件给自己).
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.',
'admin@mydomain.com',['me@gmail.com'], fail_silently=False)
Run Code Online (Sandbox Code Playgroud)
我可以进入我的重置密码视图(密码/重置/),在我给它我的电子邮件后,它正确地重定向到密码/重置/完成/但它不发送电子邮件.
这是我的urls.py:
(r'^password/reset/$','django.contrib.auth.views.password_reset'),
(r'^password/reset/done/$','django.contrib.auth.views.password_reset_done'),
(r'^password/reset/confirm/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/reset/complete/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/change/$','django.contrib.auth.views.password_change'),
(r'^password/change/done/$','django.contrib.auth.views.password_change_done'),
Run Code Online (Sandbox Code Playgroud)
这是我的password_reset_form.html:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/media/css/style_login.css" />
<title>Información de acceso requerida</title>
</head>
<body>
<div id="wrapper">
<h1>Recuperar password</h1>
<p>Utilice este formulario cuando desee recuperar el password para su usuario.</p>
{% if form.errors %}
<p>No hay un usuario registrado con ese correo electronico.</p>
{% endif %}
<form method="post" action="{% url django.contrib.auth.views.password_reset_done …Run Code Online (Sandbox Code Playgroud) 我正在使用Django 1.6开发一个Web应用程序,我要求用户使用我的登录表单登录.我想编写一个测试用例来测试登录用户的过程.
我确实成功获得了一个工作登录页面,这意味着我能够登录.以下步骤解释了我的设置.但是,为此过程编写测试用例确实失败了.在这一点上,我既不能在测试中发现错误,也不确定这种方法是否有意义.有人能帮我吗?
更新:在此期间我注意到,我的表单错误消息太不明确了.该问题似乎是由表单字段验证步骤失败的空表单字段引起的.当我将错误添加{{ form.errors }}到模板时,响应包含以下内容(格式不太好):
<ul class="errorlist">
<li>username
<ul class="errorlist">
<li>This field is required.</li>
</ul>
</li>
<li>password
<ul class="errorlist">
<li>This field is required.</li>
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我仍然不确定如何最合理地测试这个程序.我是Django的新手,也是它测试框架的哲学.可能是,这个问题不直接落入Django测试框架的范围,在这个框架中,模型,视图和表单(看似)是相互分离的(看似)?这个测试是否属于独立测试套件进行黑盒/功能测试的领域,例如,使用Selenium?
我开始是这样的:
开始了一个新项目,如下所示:
django-admin.py startproject login_test
Run Code Online (Sandbox Code Playgroud)添加了一个条目login_test/urls.py,像这样:
urlpatterns = patterns('',
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^admin/', include(admin.site.urls)),
)
Run Code Online (Sandbox Code Playgroud)制作模板/home/moooeeeep/login_test/templates/registration/login.html,如下:
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if not user.is_active %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %} …Run Code Online (Sandbox Code Playgroud)django validation django-forms django-authentication django-testing
django ×10
python ×3
django-admin ×2
django-forms ×2
django-email ×1
email ×1
gmail ×1
smtp ×1
validation ×1