Django 从模态登录 window

Django login from in modal window

我有一个登录表单,我想在 header 中输入模态 window。

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@domain.com', '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 中创建了模态 window 如何将 {% crispy form form.helper %} 放入模态 window 中? 谢谢。

UPD1: 如果我把 {% crispy form form.helper %} 放在 header.html 中,我得到错误

VariableDoesNotExist at / Failed lookup for key [form] in u'[{\'False\': False, \'None\': None, \'True\': True},

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>

Link登录应该在每个页面。

如果你想在所有页面显示表格,你需要添加form(最好叫它login_form这样它不会与你可能有的其他表格冲突)到每个查看上下文。

为了避免在所有视图中重复执行此操作,Django 具有上下文处理器。您将它们包含在 settings.pyTEMPLATES 变量中。例子

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

然后,创建一个名为 context_processors.py 的文件并在其中添加 add_my_login_form() 函数。函数returns login_form 到所有请求的请求上下文。

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

由于您要在每个页面中呈现表单,因此使用模板缓存可能会更好。