[英]Django how to use modelform to show the error messages
I use django modelform to save data, 我用Django modelform保存数据,
I use ajax ,so it won't redirect to other page. 我使用ajax,因此它不会重定向到其他页面。 when submit,the view create_post
will deal with it. 提交时,视图create_post
会处理它。
But I have a question,if I type 'abc' in the email's filed and submit. 但是我有一个问题,如果我在提交的电子邮件中输入“ abc”并提交。
I know it won't pass the validation,But it doesn't print the error message to let the user know where is the mistake. 我知道它不会通过验证,但是不会打印错误消息以使用户知道错误在哪里。
How can I edit to reach it? 如何修改才能达到目标? Please guide me. 请指导我。 Thank you . 谢谢 。
[EDIT] when I edit to this return render(request, "zh_tw/maininfo.html#5thpage",{form: 'form'})
Now if I type 'abc' in email field,the post is 200 ok. [编辑]当我编辑此return render(request, "zh_tw/maininfo.html#5thpage",{form: 'form'})
现在,如果我在电子邮件字段中输入'abc',则该帖子为200 ok。
But when it render back to this page, it will alert 'ERROR!' 但是,当它返回到此页面时,它将发出警告“ ERROR!”。
it seems like it run this part in ajax: error: function(ts){ alert('ERROR!!!'); window.location.reload(); },
似乎它在ajax中运行此部分: error: function(ts){ alert('ERROR!!!'); window.location.reload(); },
error: function(ts){ alert('ERROR!!!'); window.location.reload(); },
template/main.html template / main.html
<script type="text/javascript">
$( document ).ready(function() {
$.ajaxSetup({
data: { csrfmiddlewaretoken: '{{ csrf_token }}' },
});
// Submit post on submit
$('#create_post').on('submit', function(event){
event.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
$.ajax({
url: '{% url 'core:create_post' %}',
data: { "name":name,"email":email,"message":message,},
type: 'POST',
async: false,
dataType: 'json',
error: function(ts){
alert('ERROR!!!');
window.location.reload();
},
success: function(dataArr){
if(dataArr == 2){
alert('success!');
window.location.reload();
}else{
alert('something wrong!');
window.location.reload();
}
}
});
});
});
</script>
<div class="section" id="contact">
<div class="message">
<form action="." method="POST" id="create_post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form }}
<div>
<input type="text" name="FirstName" id="name" placeholder="your name">
<input type="text" name="Email" id="email" placeholder="your email">
</div>
<textarea placeholder="talk?" name = "Message" id="message"></textarea>
<br>
<button type="submit" value="Submit" id="submit">submit</button>
</form>
</div>
</div>
views.py views.py
def maininfo(request):
# return render(request, 'english/maininfo.html',)
return render(request, 'zh_tw/maininfo.html',)
def create_post(request):
if request.method == 'POST':
form = MessageForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.ip = request.META['REMOTE_ADDR']
obj.save()
return HttpResponse(2)
return render(request, "zh_tw/maininfo.html#5thpage",{form: 'form'})
This will render form errors and form non-field errors in case you have them. 如果您有表单错误和表单非字段错误,这将导致它们。
<div class="section" id="contact">
<div class="message">
<form action="." method="POST" id="create_post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form }}
<button type="submit" value="Submit" id="submit">submit</button>
</form>
</div>
</div>
This is a bit improved view: 这是一个改进的视图:
def create_post(request):
form = MessageForm(request.POST or None)
if form.is_valid():
obj = form.save(commit=False)
obj.ip = request.META['REMOTE_ADDR']
obj.save()
return render_to_response(request, template="your_template.html", context={form: 'form'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.