django - 将服务器错误显示为html

标签 django python-3.x error-handling django-rest-framework

我正在验证必须通过ajax发送的表单,我正在使用axios来执行此操作,但是我现在想知道是否有某种方式可以将我从服务器收到的错误显示到html中,这些都是我的错误想展示
enter image description here

我在服务器端使用Django rest-framework
这是我的代码

axios({
            method: 'post',
            url: `${baseURL}api/users/`,
            contentType: 'application/json',
            data : {} // I send the inputs data here
        })
        .then(response => {
            window.location.href = '/success.html';
            console.log(response)
        })
        .catch(err => {
            console.log(`It works ${err.message}`);
        })

这样做,我只能像这样显示消息

'It works Request failed with status code 400'

最佳答案

尝试类似:

var errors = {}
...
.then(response => {
    window.location.href = '/success.html';
    console.log(response)
    errors = Object.assign({})
})
.catch(err => {
    errors = Object.assign({}, err.data['error'])
    console.log(`It works ${err.message}`);
})

然后,您可以使用此变量来显示html中的错误,例如 errors ['address']

关于django - 将服务器错误显示为html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53643799/

相关文章:

django - 如何在 Django 模板中获得 'switch-case' 语句功能?

javascript - Django/Jquery/Javascript - 如何使用 url 参数预填充表单(自动填充表单)

python-3.x - 在非阻塞套接字上接收数据时如何解决阻塞错误

c++ - 做组合时被零除

jquery - Django post on click with jquery 没有得到响应

mysql - 使用 Pandas 插入 MySQL 替换内容

python - pyqt5 tabwidget 垂直制表符水平文本左对齐

javascript - 如何在javascript中手动抛出语法错误

error-handling - 为什么不能在词典中添加新术语?

python - 任务是导出excel数据到MySQL并显示图表!