我已经安装了django-allauth,之后这是我的settings.py
Django_apps = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Third_party_apps = (
'avatar',
'allauth',
'allauth.account',
'allauth.socialaccount',
'bootstrapform',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.twitter',
)
My_apps = ()
INSTALLED_APPS = Django_apps + Third_party_apps + My_apps
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
"django.core.context_processors.request",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_ADAPTER ="allauth.account.adapter.DefaultAccountAdapter"
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "optional"
ACCOUNT_USERNAME_MIN_LENGTH = 3
Run Code Online (Sandbox Code Playgroud)
我相信我也正确设置了我的urls.py. 而且我还有两个来自django admin的社交应用程序,带有正确的twitter和github api密钥和秘密.
但问题是每当我点击通过Twitter登录时它会向我显示正确的twitter身份验证页面,在我授权应用程序后它会给我这个错误..
<allauth.socialaccount.models.SocialLogin …Run Code Online (Sandbox Code Playgroud) 我是 Django 的新手,遇到了一个我无法解决的小问题。我使用 django-allauth 进行身份验证,使用 twitter-bootstrap 进行首页。我想创建一个无论用户是否登录都不同的页面。所以在 {% block header %} 我检查它以显示正确的按钮。但是,如果我没有登录,我什么也得不到(没有主页、新闻、仪表板和数据),如果我登录,我会看到其中的四个。请帮忙!
{% load url from future %}
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
{% block headbootstrap %}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Basic css -->
<link href="{{ STATIC_URL }}twitter_bootstrap/dist/css/bootstrap.css" rel="stylesheet">
{% endblock %}
<title>{% block head_title %}{% endblock %}</title>
{% block extra_head %}
{% endblock %}
</head>
<body>
{% block header %}
<div class="navbar navbar-fixed-top navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<!-- …Run Code Online (Sandbox Code Playgroud) 我试图使用Django与allauth尝试创建一个登录页面.
我能够成功登录并将用户重定向到LOGIN_REDIRECT_URL(="users/{id}/mypage")页面.我有/ users/{id}/mypage的单独视图,名为views.py中定义的UserHomePageView:
from django.views.generic import TemplateView
class UserHomePageView(TemplateView):
template_name = "user_homepage.html"
Run Code Online (Sandbox Code Playgroud)
我想要一个安全措施来阻止任何人(基本上是非登录用户)导航到"/ users/{id}/mypage"(例如/ users/5/mypage).ei如果未经身份验证的用户尝试导航到/ users/5/mypage,他/她应该被重定向到登录页面/主页.
基本上,我如何呈现未经身份验证的用户来查看针对经过身份验证的用户的页面(我不想使用模板标签users.authenitcated,但希望覆盖TemplateView)
我正在使用 django 1.8 和 python 3.4 我已经使用链接集成了 django allauth:http : //www.sarahhagstrom.com/2013/09/the-missing-django-allauth-tutorial/
现在,当我尝试打开http://127.0.0.1:8000/accounts/login .. 它会将我重定向到http://127.0.0.1:8000/
任何想法,为什么会发生这种情况?
我正在尝试将Google登录信息整合到我的网站中.我正在关注此示例:https://developers.google.com/identity/sign-in/web/
我也关注了http://www.marinamele.com/user-authentication-with-google-using-django-allauth,
我为我的项目生成了客户端ID并将其替换为上面的代码,我的settings.py如下:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
SITE_ID = 1
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = "none"
LOGIN_REDIRECT_URL = '/'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = \
{
'google': {
'SCOPE': ['profile','email'],
'AUTH_PARAMS': {'auth_type': 'reauthenticate', 'access_type':'online'},
'METHOD': 'oauth2',
'VERIFIED_EMAIL': False
}
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
# Required by allauth template tags
"django.core.context_processors.request",
# allauth specific context …Run Code Online (Sandbox Code Playgroud) 我用allauth模块在django上安装了一个非常基本的项目.它运作良好...除了当我尝试使用http:// localhost:8000/accounts/logout /注销时我有一个404页面 .
urls.py
urlpatterns = [
url(r'^accounts/', include('allauth.urls')),
url(r'^aboutus/$', TemplateView.as_view(template_name="aboutus.html"), name="aboutus"),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
]
并在settings.py我设置ACCOUNT_LOGOUT_ON_GET= True为避免注销确认.
我的目标是设置路径:登录页面 - > aboutus:点击退出按钮 - >登录页面.
我在网上看了很多东西,但它仍然无效.
请帮忙
我正在尝试实现包含自定义用户模型的登录。是否可以继承自allauth.account.forms.LoginForm定义字段并将其添加到自定义登录表单?
想法是通过覆盖login()方法在登录时为用户分配角色。
我已经按照allauth的配置进行了配置,并通过以下代码提到了在settings.py中用于登录的形式
AUTH_USER_MODEL = 'core.User'
ACCOUNT_SIGNUP_FORM_CLASS = 'core.forms.SignupForm'
ACCOUNT_FORMS = {'login': 'core.forms.CoreLoginForm'}
Run Code Online (Sandbox Code Playgroud)
除django.contrib.auth.backends.ModelBackend和外,我没有使用任何身份验证后端allauth.account.auth_backends.AuthenticationBackend。自定义注册对我来说工作正常,没有任何问题。但是自定义loginform不会呈现用户模型中的所有字段。根据此SO Post中接受的答案继承了Allauth LoginForm,并将choicefield添加到自定义登录表单中。
from allauth.account.forms import LoginForm
class CoreLoginForm(LoginForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(CoreLoginForm, self).__init__(*args, **kwargs)
role = forms.ChoiceField(widget=forms.Select(), choices=User.roles, initial=User.roles[0])
Run Code Online (Sandbox Code Playgroud)
在./manage.py runserver它上面说Module "core.forms" does not define a "SignupForm" class。我已经在core.forms中定义了一个SignupForm,如下所示signup will work if CoreLoginForm is inherited from forms.Form instead of LoginForm。所以如果我这样做
class CoreLoginForm(forms.Form):
def __init__(self, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的项目中加入 django-allauth。到目前为止,安装很好,但是当我尝试访问“帐户/注册”或“帐户/登录”页面时,我会自动重定向到找不到页面的帐户/个人资料
这是错误:
Page not found (404)
> Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/profile/
Using the URLconf defined in dari.urls, Django tried these URL patterns, in this order:
^accounts/ ^ ^signup/$ [name='account_signup']
^accounts/ ^ ^login/$ [name='account_login']
^accounts/ ^ ^logout/$ [name='account_logout']
^accounts/ ^ ^password/change/$ [name='account_change_password']
^accounts/ ^ ^password/set/$ [name='account_set_password']
^accounts/ ^ ^inactive/$ [name='account_inactive']
^accounts/ ^ ^email/$ [name='account_email']
^accounts/ ^ ^confirm-email/$ [name='account_email_verification_sent']
^accounts/ ^ ^confirm-email/(?P<key>\w+)/$ [name='account_confirm_email']
^accounts/ ^ ^password/reset/$ [name='account_reset_password']
^accounts/ ^ ^password/reset/done/$ [name='account_reset_password_done']
^accounts/ ^ ^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$ [name='account_reset_password_from_key']
^accounts/ ^ ^password/reset/key/done/$ [name='account_reset_password_from_key_done'] …Run Code Online (Sandbox Code Playgroud) Django 1.8.16 django-allauth 0.27.0使用postgres作为数据库。
我的应用程序不使用用户名,仅使用电子邮件地址作为用户ID。所以我使用以下设置:
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USER_MODEL_EMAIL_FIELD = 'email'
Run Code Online (Sandbox Code Playgroud)
现在,当新用户注册时,他将使用他的电子邮件地址。但是在提交注册表时,出现了以下错误:
IntegrityError at /accounts/signup/
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL: Key (username)=() already exists.
Request Method: POST
Request URL: http://swd.localhost:8000/accounts/signup/
Django Version: 1.8.16
Exception Type: IntegrityError
Exception Value:
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL: Key (username)=() already exists.
Run Code Online (Sandbox Code Playgroud)
这恰恰说明了问题所在:auth_user表的“用户名”字段中已经存在空用户名,并且似乎不允许这样做?但是问题在于,使用上述设置,用户名字段始终为空。那么我们如何解决这个问题?
我没有适应用户模型。
我开始使用 cookiecutter-django,因为它似乎比 django-admin 更先进来启动我的项目。所以我创建了一个电子商务网站,它只需要电子邮件即可登录,而不需要用户名。
因此,我尝试按照文档并像这样更改我的 settings.py:
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
AUTH_USER_MODEL = 'accounts.User'
LOGIN_REDIRECT_URL = 'users:redirect'
LOGIN_URL = 'account_login'
Run Code Online (Sandbox Code Playgroud)
这是我的帐户。用户模型:
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager
)
class UserManager(BaseUserManager):
def create_user(self, email, full_name, password=None, is_active=True, is_staff=False, is_admin=False):
if not email:
raise ValueError("Users must have an email address")
if not password:
raise ValueError("Users must have a password")
if not full_name:
raise ValueError("Users must have a fullname") …Run Code Online (Sandbox Code Playgroud) django ×10
django-allauth ×10
python ×4
html ×2
django-1.6 ×1
django-1.8 ×1
forms ×1
google-login ×1
web ×1