我如何使用django返回错误消息?我在想这样的事情(我正在使用jsonresponse作为我想要这样做的一个例子):
def test(request):
if request.method == "POST":
if form.is_valid():
form.save
return JsonResponse({"message":"Successfully published"})
else:
'''here i would like to return error something like:'''
return JsonResponse({"success": false, "error": "there was an error"})
else:
return JsonResponse({"success}: false, "error": "Request method is not post"})
Run Code Online (Sandbox Code Playgroud)
我想要实现的是从ajax错误函数在模板中呈现错误消息.像这样的东西:
$("#form").on("submit", function(){
$.ajax({url: "url",
data: ("#form").serialize,
success: function(data){
alert(data.message);
},
error: function(data){
alert(data.error);
}
});
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我正面临一个问题,如何将div中的文本(h1)居中.我希望网页是全屏的,我用vh单位实现了这一点.
.page { height: 100vh;
width: 100%;}
Run Code Online (Sandbox Code Playgroud)
在那个页面上,我希望div水平和垂直居中.我试过了
.div { top: 50%;
left: 50%;}
Run Code Online (Sandbox Code Playgroud)
但那只是没有做到的伎俩.我也希望这是"resposive"si,它以任何屏幕为中心.我希望建立这样的东西:http://www.anthonygoodisondesign.com/
我有两个基本模板:base.html 和 base_visitor.html。如果用户经过身份验证,我希望扩展base.html;如果用户未经身份验证,我希望扩展base_vistior.html。我已经尝试过这个:
{% if user.is_authenticated %}
{% extends 'base.html' %}
{% else %}
{% extends 'base_visitor.html' %}
{% endif %}
{% block title %}{{ title }}{% endblock %}
{% block body %}
<h1>Title</h1>
{% if models %}
{% for model in models %}
<h2>{{ model.model_number }}<h2>
{% endfor %}
{% else %}
<h3>NO models</h3>
{% endif %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这给了我一个错误:“第 3 行的块标记无效:'else'。您是否忘记注册或加载此标记?”
感谢帮助!