簡體   English   中英

如何使用Django的注冊字段自定義表

[英]how to use django's registration field(s) to custom table

發布時,我需要使用注冊表格的現有字段(即密碼)(不是新字段)在單獨的表上使用

我的代碼如下

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

class Token(models.Model): 
  user = models.OneToOneField(User) 
  api_key=models.CharField(max_length=50) 


def create_user_token(sender, instance, created, **kwargs): 
 if created: 
   profile, created = Token.objects.get_or_create(user=instance) 

#this will definitely not work because i have no field name api_key on form 
post_save.connect(create_user_token, sender=User) 

我創建apikey的邏輯如下

 #password=posted from django-registration form as i have used django-registration 
 digest = hmac.new('Secret', password, hashlib.sha256).hexdigest() 

我正在使用django注冊,並且每當用戶注冊或更改密碼時都需要設置api_key = digest

Django注冊有一個特殊的user_registered信號,例如,您可以從request.POST中獲取所需的信息。

要更改密碼,您還可以編寫自己的視圖(基於django的視圖)並從那里發送信號(甚至是相同的信號)。

同意Ilvar和Frantzdy的意見,您需要使用user_registered信號。 請按照以下步驟

Forms.py

from django import forms
from registration.forms import RegistrationForm
from django.utils.translation import ugettext_lazy as _
from registration.models import RegistrationProfile

attrs_dict = { 'class': 'required' }

class RegistrationFormEx(RegistrationForm):
  cell_phone = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))

models.py

import hashlib
import hmac
from django.db import models
from django.contrib.auth.models import User
from registration.signals import user_registered
from userInfo.forms import RegistrationFormEx


class ExProfile(models.Model):
   user = models.ForeignKey(User, unique=True)
   cell_phone = models.CharField(max_length=200, blank=True)
   api_key=      models.CharField(max_length=200, blank=True)

   def user_created(sender, user, request, **kwargs):
      form = RegistrationFormEx(data=request.POST)
      digest = hmac.new(str(request.POST['password1']), str(request.POST['username']), hashlib.sha256).hexdigest()
      new_user = User.objects.get(username=request.POST['username'])
      //here I have added api_key hash algo, you can change it
      new_profile = ExProfile(user=new_user,cell_phone=request.POST['cell_phone'],api_key=digest)
     new_profile.save()
     return new_user

  user_registered.connect(user_created)

urls.py

from django.conf.urls import patterns, include, url
import registration.backends.default.urls as regUrls
from registration.views import register
from userInfo.forms import  RegistrationFormEx


urlpatterns = patterns('',
url(r'^accounts/register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': RegistrationFormEx}, name='registration_register'),
(r'^accounts/', include('registration.backends.default.urls'))

) 

希望您現在就可以更改密碼,祝您好運

如果您創建一個change_password視圖,建議添加幾行代碼來更改該鍵。 出於演示目的:

profile = Token.objects.get(user=request.user)
#set a new api key
profile.api_key =hashlib.sha1(str(random.random()) +
                                   'app_name' +
                                 str(time.time())).hexdigest()
profile.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM