如何处理Django中的错误 [英] How to handle errors in Django

查看:53
本文介绍了如何处理Django中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的django应用程序尽可能对用户友好,我想处理适当的错误,并使其发出类似JavaScript中警报的错误消息.没有文件上传时,我想这样做.因此,当按下上载按钮并且没有上载任何内容时,将发出警报消息.

I want to make my django app as user friendly as possible and I want to handle appropriate errors and have it push out an error message sort of like an alert in javascript. I want to do this when there's no file uploaded. So when the upload button is pressed and nothing have been uploaded there would be an alert message sent out.

我的视图, views.py :

def upload(request):

    if "GET" == request.method:
        return render(request, 'uploadpage/upload.html', {})

    else:
        excel_file = request.FILES["excel_file"]

        # you may put validations here to check extension or file size

        wb = openpyxl.load_workbook(excel_file)

        # getting a particular sheet by name out of many sheets
        worksheet = wb['Summary']

        # iterating over the rows and
        # getting value from each cell in row

        seller_info = []
        for cells in worksheet.iter_rows(min_col=2, max_col=2, min_row=1, max_row=5):
            for cell in cells:
                seller_info.append(str(cell.value))
        return render(request, 'uploadpage/upload.html', {"excel_data": seller_info})

我的模板, uploadpage/upload.html :

  <!DOCTYPE html>

<html>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="{% static 'css/upload.css' %}">
    <head>
        <div id='banner-container'>
        <div id='banner'>
            <h1 id='header'>MY APP</h1> 
            <i class="glyphicon glyphicon-cloud" style="font-size:60px;color:lightblue;text-shadow:2px 2px 4px #000000;"></i>
        </div>
        <div>
        <body>
            <div id='upload-container' >
             <span><h1>Upload File !</h1></span>

             <span><h2>Upload Here</h2></span>

                <form method="post" enctype="multipart/form-data">
                    <div id='input'>
                            {% csrf_token %}
                        <input type="file" name="excel_file">
                        <div id='btn'><button type="submit">Upload File</button> </div>
                        </form>
                     <div>

            </div>
        </body>
        {{ excel_data }}
    </head>

</html>

推荐答案

Django为我们提供了

Django provided us a message framework which allow you to attach messages and then you can render it on your template by using JavaScript or simply just use django template.

我最喜欢的在Web应用程序上显示消息的库是 toastr .您可以转到文档页面,查看如何将其集成到项目中.

My favorite library to show message on my web application is toastr. You can go to the document page to see how you will integrate into your project.

根据您的观点:

from django.contrib import messages

# ...

def upload(request):

if "GET" == request.method:
    messages.error(request, "There's no file uploaded")
    return render(request, 'uploadpage/upload.html', {})

# ...

然后在模板上可以像这样使用它:

Then on your template you can use it like so:

...
<head>
    ...
    <link href="toastr.min.css" rel="stylesheet" />
</head>
<body>
    ...

    <script src="toastr.min.js"></script>
    {% if messages %}
        <script>
            toastr.options = {
                "showDuration": "300",
                "hideDuration": "1000",
                "timeOut": "5000"
            }
            {% for message in messages %}
                toastr.{{ message.tags }}("{{ message }}");
            {% endfor %}
        </script>
    {% endif %}
</body>

  • message.tags :用于与toastr功能匹配,例如,如果您想通过使用 messages.error(...)显示错误,则 message.tags 将是 error ,当您的模板呈现时,它变成了 toastr.error(此处的消息"),然后您会在您的浏览器中看到吐司消息.
    • message.tags: Using to match with the function of toastr, for example if you want to show an error by using messages.error(...) then the message.tags will be error, when your template rendered it turned to toastr.error("Your message here") and then you'll see the toast message on your browser.
    • 希望有帮助!

      这篇关于如何处理Django中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆