2017-11-04 7 views
0

AbstractUserを拡張してAgentというカスタムユーザーモデルを作成しました。何らかの理由でサインアップページが表示されず、なぜカスタムユーザーを作成する前にうまくいっていたのかわかりません。 AgentBasicInfoAgentPremiumInfo - 私はサインアップボタンをクリックすると、ページが登録時に作成されているAgentの上に2つの追加モデルがありAbstractUserを拡張した後のローディング(Localhostを待っています...)のサインアップページ

... localhostのを待っているに貼り付けています。 AgentBasicInfoはサインアップページに表示され、AgentPremiumInfoはバックグラウンドで作成され、登録時に実際には表示されません。

私の管理ページを確認すると、エージェントモデルが作成されていますが、AgentBasicInfoインスタンスとAgentPremiumInfoインスタンスは作成されていません。これは、agent_basic_info = basic_info_form.save(commit=False)以降に何かが詰まっていると信じていますが、私はそれが何であるか把握できません。ここで

は私のコードです:

views.py

def signup(request): 
    if request.user.is_authenticated: 
     return HttpResponseRedirect('../dashboard/') 
    if request.method == 'POST': 
     signup_form = SignupForm(request.POST) 
     basic_info_form = AgentBasicInfoForm(request.POST) 
     if signup_form.is_valid() and basic_info_form.is_valid(): 

      agent = signup_form.save(commit=False) 
      agent.is_active = False 
      agent.save() 

      # Creates a basic info form with user input 
      agent_basic_info = basic_info_form.save(commit=False) 
      agent_basic_info.agent = agent 
      agent_basic_info = agent_basic_info.save() 

      # Creates a profile model with the agent's premium information, empty except for 'agent' field. No actual form displayed on sign up page. 
      agent_premium_info = AgentPremiumInfo.objects.create(agent=agent) 
      agent_premium_info.save() 

      current_site = get_current_site(request) 
      message = render_to_string('acc_active_email.html', { 
       'agent':agent, 
       'domain':current_site.domain, 
       'uid': urlsafe_base64_encode(force_bytes(agent.pk)), 
       'token': account_activation_token.make_token(agent), 
      }) 
      mail_subject = 'Activate your blog account.' 
      to_email = signup_form.cleaned_data.get('email') 
      email = EmailMessage(mail_subject, message, to=[to_email]) 
      email.send() 
      return HttpResponse('Please confirm your email address to complete the registration') 
    else: 
     signup_form = SignupForm() 
     basic_info_form = AgentBasicInfoForm() 

    return render(request, 'signup.html', {'signup_form': signup_form, 'basic_info_form': basic_info_form}) 


def activate(request, uidb64, token): 
    try: 
     uid = force_text(urlsafe_base64_decode(uidb64)) 
     agent = Agent.objects.get(pk=uid) 
    except(TypeError, ValueError, OverflowError, Agent.DoesNotExist): 
     agent = None 
    if agent is not None and account_activation_token.check_token(agent, token): 
     agent.is_active = True 
     agent.save() 
     login(request, agent) 
     # return redirect('home') 
     return HttpResponse('Thank you for your email confirmation. Now you can login your account.') 
    else: 
     return HttpResponse('Activation link is invalid!') 

models.py

class Agent(AbstractUser): 
    pass 

class AgentBasicInfo(models.Model): 

    TITLE = (
     ('Salesperson', 'Salesperson'), 
     ('Sales Representative', 'Sales Representative'), 
     ('Broker', 'Broker'), 
     ('Broker of Record', 'Broker of Record'), 
    ) 

    agent = models.OneToOneField(Agent, on_delete=models.CASCADE) 
    agent_first_name = models.CharField(max_length=30) 
    agent_last_name = models.CharField(max_length=30) 
    agent_preferred_email = models.EmailField() 
    office_phone_number = models.CharField(max_length=10) 
    agent_brokerage = models.CharField(max_length=50) 
    agent_title = models.CharField(max_length=20, choices=TITLE) 


class AgentPremiumInfo(models.Model): 

    agent = models.OneToOneField(Agent, on_delete=models.CASCADE) 
    agent_phone_number = models.CharField(max_length=10, blank=True, null=True) 
    agent_website = models.CharField(max_length=50, blank=True, null=True) 
    agent_biography = models.TextField(blank=True, null=True) 
    agent_address_street = models.CharField(max_length=50, blank=True, null=True) 
    agent_address_city = models.CharField(max_length=25, blank=True, null=True) 
    agent_address_province = models.CharField(max_length=2, choices=PROVINCE, blank=True, null=True) # Add province choices later 
    agent_address_postal_code = models.CharField(max_length=6, blank=True, null=True) 
    agent_picture = models.ImageField(height_field=200, width_field=100, blank=True, null=True) 

forms.py

class SignupForm(UserCreationForm): 
    email = forms.EmailField(max_length=200, help_text='Required') 

    def clean_email(self): 
     data = self.cleaned_data['email'] 
     if not data.endswith('@gmail.com'): 
      raise forms.ValidationError("You must use your @gmail.com Email") 
     return data 

    class Meta: 
     model = Agent 
     fields = ('username', 'email', 'password1', 'password2') 

class AgentBasicInfoForm(forms.ModelForm): 

    class Meta: 
     model = AgentBasicInfo 
     fields = ['agent_first_name', 'agent_last_name', 'agent_preferred_email', 'office_phone_number', 'agent_brokerage', 'agent_title'] 

class AgentPremiumInfoForm(forms.ModelForm): 

    class Meta: 
     model = AgentPremiumInfo 
     fields = ['agent_phone_number', 'agent_website', 'agent_biography', 'agent_picture', 'agent_address_street', 'agent_address_city', 'agent_address_province', 'agent_address_postal_code'] 
+0

これはあなたが求めている特定の質問に対する回答ではありませんが、あなたの目標を達成するためのより簡単な方法を示すかもしれません:https://stackoverflow.com/questions/1113047/creating -a-model-and-related-models-with-inline-formsets – souldeux

答えて

0

reset_dbを実行しているにもかかわらず、データベースに問題があるようです。私は別のreset_dbを実行し、それは魔法のように問題を修正しました。

関連する問題