ユーザーが誤ったログインフォームを送信したときに、/ accounts/loginにリダイレクトするのではなく、モーダルにエラーを表示しようとしています。ログインモーダルがDjangoログインモーダルフォームのエラー表示
...
<button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#loginModal" id="login_modal_trigger">Log In</button>
{% include 'registration/login.html' with form=form %}
...
登録/ login.htmlと
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Log In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div id="content-container" class="container p-none">
<div class="lgn-container col-lg-8">
<form id="login-form" method="post"
action="{% url 'login' %}">
{% csrf_token %}
<table class="table">
<tr>
<td><label for="id_username">Username</label></td>
<td><input id="id_username" name="username"
type="text" class="form-control"></td>
</tr>
<tr>
<td><label for="id_password">Password</label></td>
<td><input id="id_password" name="password"
type="password" class="form-control"></td>
</tr>
</table>
{% if form.errors %}
<p class=" label label-danger">
Your username and password didn't match.
Please try again.
</p>
{% endif %}
<input type="submit" id="ajax_form_submit" value="Login"
class="btn btn-primary pull-right" />
<!-- I comment the next line to allow the LOGIN_REDERICT_URL in project/settings.py to work. -->
<!-- <input type="hidden" name="next" value="{{ next }}" /> -->
</form>
</div>
</div>
</div>
</div>
</div>
</div>
script.jsを表示さ
basic.html: 私はevent.preventDefault();
がエラーがある場合にリダイレクト防ぐと考えました。
$(document).ready(function() {
var frm = $('#login_form');
$('#loginModal').click("#ajax_form_submit", function(event){
event.preventDefault();
// $('#login_form').ajax_form(target:'#login_form').submit();
$.ajax({//begin
type: 'POST',
url: frm.attr('action'),
data: $(frm).serialize(),
success: function(data){
$(frm).hide();
$("#div-form").html("<h3 style='text-align:center;'>Thank you, for Registering!</h3>");
},
error: function(data) {
$("#div-form").html("Failure!");
}//end error
});//end ajax
});
});
プロジェクト/ settings.py
...
LOGIN_REDIRECT_URL = '/app/login_success/'
...
プロジェクト/ urls.py
...
urlpatterns = [
...
url(r'^accounts/', include('django.contrib.auth.urls')),
...
]
...
アプリ/ urls.py
...
app_name = 'app'
urlpatterns = [
...
url(r'^login_success/$', views.login_success, name='login_success'), # redirect to user profile
...
]
...
ログインモーダル作品が、それinstがエラーの場合は/ accounts/login urlにリダイレクトしますモーダルにエラーメッセージを表示してしまいました。私が使っているjsは、主に他の投稿のものです。
追加ライブラリなしでこれを行う方法はありますか? – FLOWMEEN
また、私はちょうどDjangoのデフォルトのログインビューを使用しているので、現在実行中のものではなくJsonResponsesを返すように書き直す必要がありますか? – FLOWMEEN