2016-11-25 87 views
4

我在我的web應用程序中使用了allauth註冊功能。在socialaccount註冊收集用戶的電子郵件,密碼,名字和姓氏。在登錄表單中,我只收集用戶的電子郵件和密碼。如何在註冊後自定義allauth中的用戶配置文件字段

一旦登錄,我的應用程序會將用戶重定向到一個配置文件頁面,如果他/她想這樣做,那麼應該更新她的配置文件(包括新的和自定義的字段,如同我添加的「機構」一樣) 。

作爲django-allauth的初學者,我想知道如何將這些新的自定義字段添加到我的用戶配置文件中,以及如何在用戶註冊後更新這些數據。

我做了什麼至今:

在settings.py

AUTH_USER_MODEL = 'auth.User' 

在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) 

在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 %} 

的代碼顯然是inco完整,因爲我沒有找到一種方法來添加字段並按照我的要求正確保存它們。由於

回答

2

我分兩步解決了這個問題,並涉及重構我張貼在我的問題代碼:

  1. 第一步(創建自定義字段USER_TABLE):

第一所有我在我的項目中添加了一個名爲CustomUser的應用程序,我在這裏處理與allauth用戶相關的任何問題。

settings.py

INSTALLED_APPS = [ 
    ... 
    'CustomUser.apps.CustomuserConfig', 
] 

AUTH_USER_MODEL = 'CustomUser.CustomUser' 

CustomUser/models.py

from __future__ import unicode_literals 

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

class CustomUser(AbstractUser): 
    add_your_custom_fields_here 

CustomUser/apps.py

from __future__ import unicode_literals 

from django.apps import AppConfig 


class CustomuserConfig(AppConfig): 
    name = 'CustomUser' 

CustomUser/admin.py

from django.contrib import admin 
from .models import CustomUser 

admin.site.register(CustomUser) 

配置您的自定義字段用戶表後,你需要在shell中運行的遷移命令添加這些字段。在我的情況下,它不起作用,然後我需要刷新我的數據庫並重新填充它。

  1. 第二步是使用新的用戶表創建更新表單。

首先,您需要覆蓋allauth中的User類。在CustomUser文件夾中創建一個form.py我們有:

from django import forms from。模型導入CustomUser

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

CustomUser /模板/ profile.html您需要創建的形式爲您的應用程序。在我來說,我創建了一個原始的一種:

Update your data <br><br> 

<form action="." method="post"> 
    {% csrf_token %} 
    {{ form }} 
    <input type="submit" value="Submit" /> 
</form> 

CustomUser/views.py你會配置您的profile.html頁面的行爲

from django.shortcuts import render 
from .forms import UpdateUserForm 
from django.http import HttpResponseRedirect 
from django.views.generic import UpdateView 
from django.core.urlresolvers import reverse_lazy 

def profile(UpdateView): 
    if request.method == 'POST': 
     form = UpdateUserForm(request.POST) 
     if form.is_valid(): 
      return HttpResponseRedirect('/accounts/profile/') 
    else: 
     form = UpdateUserForm() 

    return render(request, 'profile.html', {'form':form}) 

class UserUpdate(UpdateView): 
     form_class = UpdateUserForm 
     template_name = 'profile.html' 
     success_url = reverse_lazy('profile') 

     #get object 
     def get_object(self, queryset=None): 
      return self.request.user 

最後,只需添加輪廓的.html頁面urls.py就萬事大吉了:

from CustomUser import views 

url(r'^accounts/profile/$', views.UserUpdate.as_view() , name='profile'), 

希望它可以幫助別人...謝謝

相關問題