如何处理 Django 中的错误

到3

我想让我的 django 应用程序尽可能地用户友好,我想处理适当的错误并让它发出类似于 javascript 警报的错误消息。当没有上传文件时,我想这样做。因此,当按下上传按钮并且没有上传任何内容时,会发出警报消息。

我的观点,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 为我们提供了一个消息框架,它允许您附加消息,然后您可以使用JavaScript或仅使用 django 模板将其呈现在您的模板上。

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

关于你的看法:

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', {})

# ...

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

...
<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(...)then the message.tagswill be显示错误error,当您的模板呈现它时toastr.error("Your message here"),您将在浏览器上看到 toast 消息。

希望有帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章