我希望有一个用户系统(最好是 Django 的)来统治 Django 和 Wordpress。
用例是 Django 是嵌入到 WordPress 安装中的应用程序(通过 iframe 或类似的东西)。为了使用 Django,用户必须经过身份验证,WordPress 中的身份验证不是强制性的,但建议进行(用于发布评论和类似内容)。
为了简化网站的使用,我希望对 Django 应用程序和 WordPress 安装应用相同的注册。注册可以通过 OAuth / FB 身份验证(许多 Django 解决方案)或通过专用站点用户进行。虽然注册过程是最重要的,但如果某些用户字段能够在两个世界之间保持同步,那就太好了。
我对此事的想法:
以前有人遇到过这种情况吗?任何建议将不胜感激。
我在Django中有两个模型:User(由Django预定义)和UserProfile.这两个通过外键连接.
models.py:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True, related_name="connect")
location = models.CharField(max_length=20, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
我正在使用UserCreationForm(由Django预定义)用于用户模型,并在forms.py中为UserProfile创建了另一个表单
#UserCreationForm for User Model
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
exclude = ("user", )
Run Code Online (Sandbox Code Playgroud)
我在模板,registration.html中加载这两个表单,因此网站客户可以输入有关两个模型中包含的字段的数据(例如:"first_name",用户模型中的"last_name",UserProfile模型中的"location").
对于我的生活,我无法弄清楚如何为此注册表单创建一个视图.到目前为止我所尝试的将创建User对象,但它不会将相应的其他信息(如位置)关联到相应的UserProfile对象中.谁能帮我吗?这就是我目前拥有的:
def register(request):
if request.method == 'POST':
form1 = UserCreationForm(request.POST)
form2 = UserProfileForm(request.POST)
if form1.is_valid():
#create initial entry for User object
username = form1.cleaned_data["username"]
password = form1.cleaned_data["password"]
new_user = User.objects.create_user(username, password)
# What to do here to save "location" field in a UserProfile
# object that corresponds with the …Run Code Online (Sandbox Code Playgroud) 我有两种用户:第一种用户必须付费才能注册到该站点,并且他可以执行一系列操作。在第二个可以注册和登录免费(与他们的Facebook或谷歌或用户名和密码),并可以执行不同的一套动作。
两者都需要有个人资料和自定义字段,当然,用户类型会有所不同。
django django-profiles django-registration django-users django-allauth
我想将我的自定义 TrustAdministration 字段添加到 django 中的 Auth_User 表
这是我的Model.py
from django.db import models
from django.contrib.auth.models import User
class Trust(models.Model):
name = models.CharField(max_length=200)
class TrustAdministration(models.Model):
user = models.OneToOneField(User)
full_name = models.CharField(max_length=200)
email = moels.CharField(max_length=75)
Run Code Online (Sandbox Code Playgroud)
表单.py
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from models import Trust, TrustAdministration
class TrustForm(form.ModelForm):
class Meta():
model = Trust
class TrustAdministration(forms.ModelForm):
class Meta():
model = TrustAdministration
class UserCreateForm(UserCreationForm):
class Meta():
model = User
Run Code Online (Sandbox Code Playgroud)
查看.py
def trust_registration(request):
if request.POST:
trust_form = TrustForm(request.POST, instance=Trust())
trust_admin_form = TrustAdministrationForm(request.POST,instance=TrustAdministration()) …Run Code Online (Sandbox Code Playgroud) django django-forms django-views django-authentication django-users
我的项目名称是timecapture
这是timecapture/settings.py的相关部分
INSTALLED_APPS = [
# 'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'timecapture',
'timesheet'
]
AUTH_USER_MODEL = 'timecapture.TimeUser'
Run Code Online (Sandbox Code Playgroud)
这里是timecapture/models.py
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.utils import timezone
class TimeUserManager(BaseUserManager):
use_in_migrations = True
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
) …Run Code Online (Sandbox Code Playgroud) 我创建了User继承自AbstractUserin 的自定义模型Django:
class ChachaUser(AbstractUser):
birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
Run Code Online (Sandbox Code Playgroud)
和我的CustomUserCreationForm:
GENDER_CHOICES = (
('M', '?'),
('F', '?'),
)
class MyUserCreationForm(UserCreationForm):
birth = forms.DateField(
widget=forms.SelectDateWidget(
years=range(1970, 2015)
),
required=True,
)
gender = forms.ChoiceField(choices=GENDER_CHOICES, initial='M')
class Meta(UserCreationForm.Meta):
model = ChachaUser
fields = UserCreationForm.Meta.fields + ('birth', 'gender')
Run Code Online (Sandbox Code Playgroud)
但是我想使用 创建一个超级用户python manage.py createsuperuser,我也必须实现CustomUserManager。
有什么想法或例子吗?
我正在尝试使用基于类的视图,而我最终得到的是默认的 403 Forbidden 页面..mixin 类的顺序是否正确?代码是否甚至在 get/post 中被使用 - 还是所有内容都被绕过并发生默认的 403 重定向?
到目前为止看到的所有工作示例,仅指向基于函数的视图中的装饰器 @login_required 并使用请求对象重定向到登录页面。该文件提供了一些技巧,但我不能得到它与下面的代码工作..把错误堆栈为好。
查看
"GET / HTTP/1.1" 200 2580
Forbidden (Permission denied): /app/custom-view
Traceback (most recent call last):
File "C:\Users\me\.envs\project\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\me\.envs\project\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\me\.envs\project\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\me\.envs\project\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\me\.envs\project\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
return super().dispatch(request, *args, …Run Code Online (Sandbox Code Playgroud) django django-views django-authentication django-users django-class-based-views
如何在 django 用户模型中添加新字段。django 用户模型带有标准的用户名电子邮件和密码,但是我希望用户有更多与他们的帐户相关联的详细信息,例如电话号码或其他内容。什么是最好的方法来做到这一点?
我需要有一个函数告诉我django会话ID当前是否已经过身份验证.我知道这已经内置到django中,我的工作正常.
但是我有一个传递会话ID的外部应用程序,当它将会话ID字符串传递回django时,我需要验证此会话ID是否有效并且当前已经过身份验证.
我在哪里开始重用django 1.2的一些内置函数?
谢谢
python django django-authentication django-sessions django-users
我目前正在设计一个基于Django的网站.为简单起见,我们假设它是一个简单的社区站点,用户可以登录并向其他用户写入消息.
我目前的选择是使用buildin用户模型或构建我自己的东西.我不需要很多来自buildin User:没有用户名(你的电子邮件地址是你的用户名),但你设置了一个你可以选择的内部名称,可供多个用户(如Facebook)使用.此外,我不需要权限系统,因为访问其他人不会基于组.因此,我最终只使用buildin中的电子邮件,名字,姓氏和密码字段,User其他所有内容都将放在UserProfile中.另一方面,buildin用户系统将在网站的后端派上用场,因为我有可能需要一个基于组的权限系统.
总而言之,在我看来,我宁愿构建我的一个用户模型并仅使用buildin来访问管理员后端.
我的思考有什么不对吗?