Django错误:在赋值之前引用了本地变量'image'

2024-06-08 01:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django代码中有一个错误。这是回溯:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\dell\Desktop\BlrCityPolice\fir\views.py" in create_image
  123.         file_type = image.image_file.url.split('.')[-1]

Exception Type: UnboundLocalError at /fir/2/create_image/
Exception Value: local variable 'image' referenced before assignment:

在视图.py公司名称:

^{pr2}$

创建_图像.html公司名称:

{% extends 'fir/base.html' %}
{% block title %}Add a New Image{% endblock %}
{% block evidences_active %}active{% endblock %}

{% block body %}
<div class="container-fluid">

    <div class="row">

        <!-- Left Evidence Info -->
        <div class="col-sm-4 col-md-3">
            <div class="panel panel-default">
                <div class="panel-body">
                        <a href="{% url 'fir:detail' evidence.id %}">
                            {% if evidence.informant_image %}
                                <img src="{{ evidence.informant_image.url }}" class="img-responsive">
                            {% else %}
                                <h3>No image to display</h3>
                            {% endif %}
                        </a>
                    <h1>{{ evidence.crime }} <small>{{ evidence.complaint }}</small></h1>
                    <h2>{{ evidence.informant_name }}</h2>
                </div>
            </div>
        </div>

        <!-- Right Image Info -->
        <div class="col-sm-8 col-md-9">

            <ul class="nav nav-pills" style="margin-bottom: 10px;">
                <li role="presentation"><a href="{% url 'fir:detail' evidence.id %}">View All</a></li>
                <li role="presentation" class="active"><a href="{% url 'fir:create_image' evidence.id %}">Add New Image</a></li>
            </ul>

            <div class="panel panel-default">
                <div class="panel-body">
                    <h3>Add a New Image</h3>
                    {% if error_message %}
                        <p><strong>{{ error_message }}</strong></p>
                    {% endif %}
                    <form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        <input type="hidden" name="evidence" value="{{ evidence }}">
                        {% include 'fir/form_template.html' %}
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

    </div>

</div>

{% endblock %}

形式_模板.html公司名称:

{% for field in form %}
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <span class="text-danger small">{{ field.errors }}</span>
        </div>
        <label class="control-label col-sm-2" for="image_title">{{ field.label_tag }}</label>
        <div class="col-sm-10">{{ field }}</div>
    </div>
{% endfor %}

在图像.html公司名称:

{% extends 'fir/base.html' %}
{% block title %}All Images{% endblock %}
{% block images_active %}active{% endblock %}

{% block body %}
<div class="container-fluid multimedia-container">

    <div class="row">
        <div class="col-sm-12">
            <ul class="nav nav-pills" style="margin-bottom: 10px;">
                <li role="presentation" {% if filter_by == 'all' %}class="active"{% endif %}><a href="{% url 'fir:images' 'all' %}">View All</a></li>
            </ul>
        </div>
    </div>

    <div class="row">

        <div class="col-sm-12">

            <div class="panel panel-default">
                <div class="panel-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Image Title</th>
                                <th>Informant name</th>
                                <th>Image File</th>
                                <th>Evidence</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for image in image_list %}
                                <tr>
                                    <td>{{ image.image_title }}</td>
                                    <td>{{ image.evidence.informant_name }}</td>
                                    <td>
                                        <a target="_blank" href="{{ image.image_file.url }}">
                                            <button type="button" class="btn btn-success btn-xs">
                                                <span class="glyphicon glyphicon-play"></span>&nbsp; Play
                                            </button>
                                        </a>
                                    </td>
                                    <td>
                                        <a href="{% url 'fir:detail' image.evidence.id %}">
                                            <img src="{{ image.evidence.informant_image.url }}" class="img-responsive" style="width: 20px; float: left; margin-right: 10px;" />
                                        </a>
                                        <a href="{% url 'fir:detail' image.evidence.id %}">{{ image.evidence.crime }}</a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>

</div>
{% endblock %}

Tags: imagedivformurlhtmlcolblockclass
1条回答
网友
1楼 · 发布于 2024-06-08 01:30:03

罪魁祸首是以下几行-

image = form.save(commit=False)
image.evidence = evidence
image.image_file = request.FILES['image_file'

你应该纠正如下:

^{pr2}$

在这里,您尝试查看表单中发送的图像标题是否已经存在于db中,如果已经存在,则呈现一个页面。但后面的行是错误的,因为它永远不会因为呈现而被调用。一旦您更正了调用它的上下文,您的程序应该可以正常工作。在

相关问题 更多 >

    热门问题