注册后如何在allauth中自定义用户配置文件字段

Gui*_*nha 5 django django-allauth

我使用allauth来注册Web应用程序中的功能。在社交帐户中,注册是从用户的电子邮件,密码,名字和姓氏中收集的。在登录表单中,我只是收集用户的电子邮件和密码。

登录后,我的应用程序会将用户重定向到个人资料页面,如果他/她想这样做,他/她应该在该页面上更新她的个人资料(包括新的和自定义的字段,作为我添加的“机构”)。

作为django-allauth的初学者,我想知道如何将这些新的自定义字段添加到我的用户个人资料中,以及如何在用户注册后更新这些数据。

我到目前为止所做的:

在settings.py中

AUTH_USER_MODEL = 'auth.User'
Run Code Online (Sandbox Code Playgroud)

在my_project / models.py中

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):

    institution = models.TextField(max_length=254)
Run Code Online (Sandbox Code Playgroud)

在profile_page.html中

{% extends 'base.html' %}
{% block title %}Profile Page{%endblock title%}

{% if user.is_authenticated %}

{%block body%}

    <div class="col-md-6" >
        <div class="panel panel-default">
            <div class="panel-body">
                <h1 class="text-center"><b>MY ONTOLOGIES</b></h1><br>
            </div>
        </div>
    </div>
    <div class="col-md-6" >
        <div class="panel panel-default">
            <div class="panel-body">
                <h1 class="text-center"><b>MY PROFILE</b></h1><br>
                    <div class="form-group">
                        {% csrf_token %}
                        <label for="id_login" class="col-sm-2 control-label">E-mail:</label>
                        <div class="col-sm-10">
                            <input type="email" id="asd" value="{{user.email}}" name="login"   class="form-control" required />
                        </div><br><br>
                        <label for="first_name" class="col-sm-2 control-label">First Name:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_first_name" value="{{user.first_name}}" name="first_name" class="form-control" required />
                        </div><br><br>
                        <label for="last_name" class="col-sm-2 control-label">Last Name:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_last_name" value="{{user.last_name}}" name="last_name" class="form-control" required />
                        </div><br><br>
                        <label for="institution" class="col-sm-2 control-label">Institution:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_institution" value="{{user.institution}}" name="institution" class="form-control" placeholder="University/Company" required />
                        </div><br><br>
                        <label for="id_password1" class="col-sm-2 control-label">New Password:</label>
                        <div class="col-sm-10">
                            <input class="form-control" id="id_password1" name="password1" placeholder="New Password" type="password" />
                        </div><br><br>
                        <label class="col-sm-2" for="id_password2">New Password (again):</label>
                        <div class="col-sm-10">
                            <input class="form-control" id="id_password2" name="password2" placeholder="New Password (again)" type="password" />
                        </div>

                    </div>
            </div>
        </div>
    </div>

{%endblock body%}

{% endif %}
Run Code Online (Sandbox Code Playgroud)

该代码显然是不完整的,因为我没有找到一种方法来添加字段并按我的要求正确保存它们。谢谢

Gui*_*nha 4

我分两步解决了这个问题,并涉及重构我在问题中发布的代码:

\n\n
    \n
  1. 第一步(为 user_table 创建自定义字段):
  2. \n
\n\n

首先,我在我的项目中添加了一个名为 CustomUser 的应用程序,在其中处理与 allauth 用户相关的任何问题。

\n\n

设置.py

\n\n
INSTALLED_APPS = [\n    ...\n    \'CustomUser.apps.CustomuserConfig\',\n]\n\nAUTH_USER_MODEL = \'CustomUser.CustomUser\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

CustomUser/models.py中

\n\n
from __future__ import unicode_literals\n\nfrom django.contrib.auth.models import AbstractUser\nfrom django.db import models\n\nclass CustomUser(AbstractUser):\n    add_your_custom_fields_here \n
Run Code Online (Sandbox Code Playgroud)\n\n

CustomUser/apps.py中

\n\n
from __future__ import unicode_literals\n\nfrom django.apps import AppConfig\n\n\nclass CustomuserConfig(AppConfig):\n    name = \'CustomUser\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

在 CustomUser/admin.py 中

\n\n
from django.contrib import admin\nfrom .models import CustomUser\n\nadmin.site.register(CustomUser)\n
Run Code Online (Sandbox Code Playgroud)\n\n

将自定义字段配置到用户表后,您需要在 shell 中运行 migrate 命令来添加这些字段。就我而言,它不起作用,然后我需要刷新数据库并再次填充它。

\n\n
    \n
  1. 第二步是使用新用户表创建更新表单。
  2. \n
\n\n

首先,您需要重写 allauth 中的 User 类。在 CustomUser 文件夹中创建一个 form.py 我们有:

\n\n

从 django 导入表单\n从 .models 导入 CustomUser

\n\n
class UpdateUserForm(forms.ModelForm):\n    class Meta:\n        model = CustomUser\n        # Add here the fields you want to be present in the update form\n        fields = (\'first_name\', \'last_name\', \'institution\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

CustomUser/templates/profile.html中,您需要为您的应用程序创建表单。就我而言,我创建了一个原始的:

\n\n
Update your data <br><br>\n\n<form action="." method="post">\n    {% csrf_token %}\n    {{ form }}\n    <input type="submit" value="Submit" />\n</form> \n
Run Code Online (Sandbox Code Playgroud)\n\n

CustomUser/views.py中,您将配置 profile.html 页面的行为

\n\n
from django.shortcuts import render\nfrom .forms import UpdateUserForm\nfrom django.http import HttpResponseRedirect\nfrom django.views.generic import UpdateView\nfrom django.core.urlresolvers import reverse_lazy\n\ndef profile(UpdateView):\n    if request.method == \'POST\':\n        form = UpdateUserForm(request.POST)\n        if form.is_valid():\n            return HttpResponseRedirect(\'/accounts/profile/\')\n    else:\n        form = UpdateUserForm()\n\n    return render(request, \'profile.html\', {\'form\':form})\n\nclass UserUpdate(UpdateView):\n        form_class = UpdateUserForm\n        template_name = \'profile.html\'\n        success_url = reverse_lazy(\'profile\')\n\n        #get object\n        def get_object(self, queryset=None): \n            return self.request.user\n
Run Code Online (Sandbox Code Playgroud)\n\n

最后,只需将 profile.html 页面添加到urls.py并 voil\xc3\xa0 即可:

\n\n
from CustomUser import views\n\nurl(r\'^accounts/profile/$\',  views.UserUpdate.as_view() , name=\'profile\'), \n
Run Code Online (Sandbox Code Playgroud)\n\n

希望它可以帮助别人...谢谢

\n