我想问题在于modal的表单中缺少submit按钮,除非您通过JS提交表单。
PasswordChangeForm
通过添加上下文处理器在基本模板中创建实例。
我创建了一个文件
context_processors.py
在我的应用程序文件夹中(在您的情况下是
usermgmt
)新的上下文处理器
password_form
(您可以自由使用任何名称,只需确保在
settings.py
,更多详细信息):
from django.contrib.auth.forms import PasswordChangeForm
def password_form(request):
return {
'pwd_form': PasswordChangeForm(request.user),
}
然后我把一条线插入核心
settings.py
TEMPLATES/OPTIONS/context\u processors部分,现在看起来像这样:
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'usermgmt.context_processors.password_form'
],
},
pwd_form
在任何模板中,包括
base.html
. 我按照
base.html
<div id="passwordChangeModal" class="modal fade" role="dialog">
<div class="modal-dialog" role="document"> <!-- without this div I wasn't able to close opened modal -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Change Password</h4>
</div>
<div class="modal-body">
<form method="post" action="{% url 'change_password' %}" class="form">
{% csrf_token %}
{{ pwd_form }}
<button type="submit" class="btn btn-default">Save</button> <!-- note lack of data-dismiss="modal" - with this attr set I wasn't able to submit form as modal just got closed all the time - probably because of the precedence of modal-related attrs, but I couldn't find any docs regarding this behaviour -->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>