在模式窗口中从Django登录 [英] Django login from in modal window

查看:75
本文介绍了在模式窗口中从Django登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a login form and I want to put in modal window in header.

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

/templates/account/login/index.html

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

我已经在 /templates/layouts/header.html中创建了模态窗口
如何在模式窗口中放入 {%crispy form form.helper%}
谢谢。

I have created modal window in /templates/layouts/header.html How can I put {% crispy form form.helper %} in the modal window? Thanks.

UPD1:
如果我输入 {%crispy form form.helper header.html中的%} 出现错误


VariableDoesNotExist at /在以下位置查找键[form]失败u'[{\'False\':False,\'None\':None,\'True\':True},

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

UPD2:
模式形式:

UPD2: Modal form:

    <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 to login should be in every page.

推荐答案

如果要在所有页面中显示该表单,则需要添加表单(最好将其命名为 login_form ,这样就不会与每个View的上下文冲突)。

If you want to display the form in all pages, you need to add the form (better call it login_form so it doesn't conflict with other forms you may have) to every View's context.

为避免在重复执行此操作在所有视图中,Django都有上下文处理器。将它们包含在 settings.py TEMPLATES 变量中。示例

To avoid doing that repeatedly in all Views, Django has Context Processors. You include them in settings.py's TEMPLATES variable. Example

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

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

Then, create a file called context_processors.py and add the add_my_login_form() function in there. The function returns the login_form to the request context of all requests.

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

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

Since you are rendering the form in every page, it maybe good to use template caching.

这篇关于在模式窗口中从Django登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆