我有一个登录表单,我想在标题中放入模态窗口。
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
创建了模态窗口
如何将{% crispy form form.helper %}
放入模态窗口?
感谢。
UPD1:
如果我将{% crispy form form.helper %}
放入header.html,我收到了错误
VariableDoesNotExist在/ [39]中{/ \&#39; False \&#39;:False,\&#39;无\&#39;:无,\& #39; True \&#39;: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">×</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 :(得分:3)
如果您想在所有页面中显示该表单,您需要添加form
(更好地称之为login_form
,以便它不会与您可能拥有的其他表单发生冲突)查看背景信息。
为避免在所有视图中重复执行此操作,Django具有上下文处理器。您将它们包含在settings.py
的{{1}}变量中。实施例
TEMPLATES
然后,创建一个名为TEMPLATES = [{
...
'OPTIONS': {
'context_processors': [
...
'myapp.context_processors.add_my_login_form',
],
}]
的文件,并在其中添加context_processors.py
函数。该函数将add_my_login_form()
返回到所有请求的请求上下文。
login_form
由于您在每个页面中呈现表单,因此使用模板缓存可能会很好。