Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用django';s注册字段到自定义表_Python_Django_Registration - Fatal编程技术网

Python 如何使用django';s注册字段到自定义表

Python 如何使用django';s注册字段到自定义表,python,django,registration,Python,Django,Registration,我需要使用登记表的现有字段(即密码)(这不是新字段),以便在过账时在单独的表格上使用 我的代码如下 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)

我需要使用登记表的现有字段(即密码)(这不是新字段),以便在过账时在单独的表格上使用

我的代码如下

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注册有一个特殊信号,您可以从
请求中获取所需信息。例如,POST


要更改密码,您可以编写自己的视图(基于django的视图)并从那里发送信号(甚至可能是相同的信号)。

django注册有一个特殊的信号,您可以从
请求中获取所需信息。例如,POST


要更改密码,您可以编写自己的视图(基于django的视图),并从那里发送信号(甚至可能是相同的信号)。

如果您创建更改密码视图,我建议添加几行代码来更改该密钥。 出于演示目的:

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


如果您创建了更改密码视图,我建议添加几行代码来更改该密钥。 出于演示目的:

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


同意Ilvar和Frantzdy的意见,您需要使用用户注册信号。按照以下步骤操作

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)
url.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'))

) 

希望您现在就可以更改密码,祝您好运

同意Ilvar和Frantzdy,您需要使用用户注册信号。按照以下步骤操作

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)
url.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'))

) 
希望你现在就可以修改密码,祝你好运