Django从模态窗口登录

标签 django django-forms django-templates django-views django-crispy-forms

我有一个登录表单,我想在标题中放入模态窗口。

网址.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)),

View .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)

表格.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 中创建了模态窗口
怎么放{% crispy form form.helper %}在模态窗口中?
谢谢。

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>

登录链接应该在每个页面中。

最佳答案

如果要在所有页面中显示表单,则需要添加form (最好将其称为 login_form,因此它不会与您可能拥有的其他形式冲突)到每个 View 的上下文。

为了避免在所有 View 中重复这样做,Django 有上下文处理器。您将它们包含在 settings.py 中的 TEMPLATES多变的。例子

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

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

关于Django从模态窗口登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982412/

相关文章:

javascript - js 向 django 传递参数

python - 一对一字段 Django Admin

python - 获取不属于不同模型的多对多关系的对象?

python - Django - 将值从模板传递到 View 函数

python - 在 Django 模板中使用 with 和 add 组合变量失败

Javascript 在表中显示更多按钮

django - 在 Google 应用引擎中找不到 ImportError : django. 实用程序

python - Django模型表单: Exclude a field which is not in Model

python - 重新显示时未显示初始数据的 Django ModelForm

css - 无法连续查看多个缩略图