4

私はログインフォームを持っており、ヘッダーにモーダルウィンドウを入れたいと思います。Djangoのモーダルウィンドウからのログイン

Urls.py

url(r'^account/login/$', appuser_views.LoginView.as_view(template_name = 'account/login/index.html', form_class = appuser_forms.LoginForm, target_url = LOGIN_TARGET_URL)), 

views.py

class LoginView(ResendEmailToUsersMixin, AjaxFormView): 

    def process_form(self, request, form): 
     data = form.cleaned_data 

     a = AppUserCredential.objects.select_related('appuser').filter(
       data1 = data['email_address'], 
       credential_type = AppUserCredential.CREDENTIAL_TYPES.EMAIL_PASSWORD, 
       appuser__status = AppUser.STATUS_TYPES.Active 
      ).first() 

     force_error = False 
     if a is None: 
      response = self.resend_email_to_users(data = data) 
      if response is not None: 
       return response 
      #If no email exists force the error and don't check the password 
      force_error = True 

     if force_error or not a.check_password(data['password']): 
      return AjaxErrorResponse(code="login_error", title="Username or Password Error", message="The username or password you have provided don’t match our records. Please check your entries and try again.") 

     a.appuser.login(request) 

forms.py

class LoginForm(AjaxForm): 
    email_address = forms.EmailField(label = "Email Address", required = True, max_length = 100, 
        widget = forms.TextInput(attrs = {'placeholder': '[email protected]', 'autocomplete':'off'})) 
    password = forms.CharField(label = "Password", required = True, max_length = 100, 
        widget = forms.PasswordInput(attrs = {'placeholder': 'Password', 'autocomplete':'off'})) 

    def setup_form_helper(self, helper): 
     helper.form_id = 'login_form' 
     helper.layout = Layout(
      'email_address', 
      'password', 
      Div(
       Submit('submit', 'Login', css_class='btn btn-primary'), 
       css_class="form-group text-center" 
       ), 
      HTML('<p class="pull-right light-top-bottom-padding"><a href="/account/forgot-password" title="Forgot Password">Forgot Password?</a></p>') 
      ) 

/templates/account/login/index.html

... 
{% crispy form form.helper %} 
... 

モーダルウィンドウを作成しました/templates/layouts/header.html {% crispy form form.helper %}をモーダルウィンドウに配置するにはどうすればよいですか?おかげさまで

UPD1: 私はheader.htmlで{% crispy form form.helper %}を入れた場合、私はuの中でキー[フォーム]のために失敗した検索/時エラー

VariableDoesNotExistを得た '[{\' 偽\ ':Falseに、 \ 'なし\': '真\' \なし、:真}、

UPD2: モーダルフォーム:

<a href="javascript:void(0);" class="text-btn" data-toggle="modal" data-target="#login">login</a> 

     <div id="login" class="modal fade" role="dialog"> 
      <div class="modal-dialog"> 

      <!-- Modal content--> 
      <div class="modal-content"> 
       <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 
       <h4 class="modal-title">Modal Header</h4> 
       </div> 
       <div class="modal-body"> 
        <p>Some text in the modal.</p> 
        <div class="panel-body"> 
         <header class="section-title text-center normal-top-bottom-padding"> 
          <h1>Login</h1> 
         </header> 
        </div> 

       </div> 
       <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 

      </div> 
     </div> 

すべてのページにログインする必要があります。

+0

フォームをモーダルウィンドウのテンプレート領域内にレンダリングするだけです。そのモーダルウィンドウはどうやって作りましたか?コードを投稿してください。 – C14L

+0

これは単なるbootsrapモーダルです。私は自分の投稿を更新しました@ C14L –

+0

どこから 'AjaxFormView'を取得しましたか? –

答えて

3

フォームをすべてのページに表示する場合は、form(それは他のフォームと競合しないように、login_formという名前を付けてください)を追加する必要があります。

Djangoには、すべてのビューでこれを繰り返すことを避けるため、コンテキストプロセッサがあります。それらをsettings.pyTEMPLATES変数に含めます。例次に

TEMPLATES = [{ 
    ... 
    'OPTIONS': { 
     'context_processors': [ 
      ... 
      'myapp.context_processors.add_my_login_form', 
     ], 
    }] 

context_processors.pyというファイルを作成し、そこにadd_my_login_form()機能を追加します。この関数は、すべての要求の要求コンテキストにlogin_formを返します。

def add_my_login_form(request): 
    return { 
     'login_form': LoginForm(), 
    } 

すべてのページでフォームをレンダリングするので、テンプレートキャッシングを使用するとよいでしょう。

+0

しかし次に何? add_my_login_form()の使用方法とURLパターンの設定方法は? –

+0

ありがとう!私はそれが動作すると思います。ログインしようとするとエラー405(メソッドが許可されていません)が表示されました。 –

+0

フォームターゲットビューは 'POST'リクエストを受け入れますか? – C14L

関連する問題