0

グループシーカーとユーザー登録を作成する必要があり、シーカーに自動的に追加する必要があります


models.py

class Seeker(models.Model):
       user = models.OneToOneField(User)
       birthday = models.DateField()
       name = models.CharField(max_length=100)

        def __unicode__(self):
           return self.name

フォーム.py

これは、アプリケーションのユーザー ID とパスワードを保存するためのデフォルトのユーザーです

class RegistrationForm(ModelForm):
     username = forms.CharField(label = (u'User Name'))
     email = forms.EmailField(label =(u'Email Address'))
     password = forms.CharField(label = (u'Password'),widget = forms.PasswordInput(render_value = False))
     password1 = forms.CharField(label =(u'Verify Password'),widget = forms.PasswordInput(render_value = False))

   class Meta:
       model = Seeker
       exclude = ('user',)

   def clean_username(self):
       username = self.cleaned_data['username']
       try:
          User.objects.get(username = username)
       except User.DoesNotExist:
       return username

       raise forms.ValidationError("That username is already taken,please select another.")

   def clean(self):

      if self.cleaned_data['password'] != self.cleaned_data['password1']:
           raise forms.ValidationError("The Password did not match please try again.")
      return self.cleaned_data

ビュー.py

ユーザーの作成にデフォルトのユーザーを使用しています

def SeekersRegistration(request):
   if request.user.is_authenticated():
          return HttpResponseRedirect('/profile/')
   if request.method == "POST":
      form = RegistrationForm(request.POST)
      if form.is_valid():
        user = User.objects.create_user(username = form.cleaned_data['username'], email = form.cleaned_data['email'], password =form.cleaned_data['password'])
        user.save()
        seekers = Seeker(user =user, name = form.cleaned_data['name'],birthday = form.cleaned_data['birthday'])
        seekers.save()
        return HttpResponseRedirect('/profile/')
    else:
        return render_to_response('register.html',{'form':form},context_instance = RequestContext(request))
else:
    '''user is not submitting the form, show them a blank registration form'''
    form = RegistrationForm()
    context = {'form':form}
    return render_to_response('register.html',context,context_instance = RequestContext(request))
4

1 に答える 1

0

信号を使用する

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

@receiver(post_save, sender=User)
def add_user_to_specific_group(instance, created, **kwargs):
    if created:
        # assign user to group
于 2012-08-23T12:49:12.193 に答える