如何使用渲染器更改路径

时间:2019-01-08 12:58:49

标签: python django django-urls

我正在研究django应用程序,该应用程序在两个不同的模板中包含两种形式。在第一种形式中,可以上传图像。上载图像后,图像分类器将图像分类并使用第二种形式呈现模板。在第二个表单中,填写了有关图像的一些描述,然后提交了表单。然后保存该表单,并可以在其他模板中对其进行查看。

urls.py

urlpatterns = [
    path('', views.index, name='home'),
    path('accounts/', include('django.contrib.auth.urls')),
    path('signup/', views.signup, name='signup'),
    path('items/', views.item_list, name='item_list'),
    path('items/upload/', views.upload_image, name='upload_image'),
    path('items/upload/description/', views.upload_item, name='upload_item'),
    path('items/<int:pk>/', views.delete_item, name='delete_item'),
    path('items/upload/image_classification/', views.image_classification, name='image_classification'),
]

在图像上传后的第一种形式中,路径变为items/upload/image_classification/并呈现第二种形式,根据urls.py,其形式为path('items/upload/description/', views.upload_item, name='upload_item')

我面临的问题是,当我尝试保存第二种表单时,将运行views.py中的 image_classification 函数,而理想情况下应运行 upload_item 函数。这导致表单无法保存。

当我尝试不使用第一个表单而直接加载第二个表单时,路径为'items/upload/description/',并且成功保存了该表单。

我在做什么错了?

这是其余的代码

views.py

def upload_image(request):
    return render(request, 'upload_image.html')

def upload_item(request):
    if request.method == 'POST':
        form_des = ItemForm(request.POST, request.FILES)

        if form_des.is_valid():
            form_des.save()
            return redirect('item_list')
    else:
        form_des = ItemForm()

    return render(request, 'upload_item.html', {'form_des': form_des})

def handle_uploaded_file(file, filename):
    if not os.path.exists('media/classification/'):
        os.mkdir('media/classification/')

    with open('media/classification/' + filename, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

def image_classification(request):

    form_des = ItemForm()

    if request.method == 'POST':
        if 'file' in request.FILES:
            handle_uploaded_file(request.FILES['file'], str(request.FILES['file']))

            img = np.expand_dims(cv2.resize(cv2.imread(os.path.join('./media/classification/', str(request.FILES['file']))), (170, 100)), axis=0)
            cat_prediction = cat_classifier.predict_classes(img)[0]
            pattern_prediction = pat_classifier.predict_classes(img)[0]

            form_des.fields['title'].widget.attrs['value'] = cat_prediction
            form_des.fields['pattern'].widget.attrs['value'] = pattern_prediction

            return render(request, 'upload_item.html', {'form_des': form_des})

    return HttpResponse('failed')

upload_image.html模板

{% block content %}
<div class="row justify-content-center">
  <div class="col-8">
    <center>
      <h2>Upload image</h2>
    <div class="row justify-content-center">
        <div class="upload-btn-wrapper">
          <form action="{{ request.build_absolute_uri }}image_classification/" method="POST" enctype="multipart/form-data" class='first_form'>
            {% csrf_token %}
            <input type="file" name="file" id="file" class="inputfile" />
            <label for="file" class="btn btn-outline-warning btn-lg btn-block select mt-5">Choose a file</label>

            <input class='btn btn-primary btn-lg btn-block' type="submit" value="Upload image" />
          </form>
        </div>
    </div>
    <p class='font-weight-bold mt-5'>Step: 1 of 2</p>
    </center>
  </div>
</div>
{% endblock content %}

upload_item.html模板

{% block content %}
<div class="row justify-content-center">
  <div class="col-6">
    <center>
      <h2>Item description</h2>

    </center>
    <div class="card mb-5 mt-3">
      <div class="card-body">
        <form method="post" enctype="multipart/form-data">
          {% csrf_token %}
          {{form_des|crispy}}
          <button type="submit" class='btn btn-primary btn-lg btn-block save_btn'>Save item</button>
        </form>
      </div>
    </div>
    <center><p class='font-weight-bold mt-5'>Step: 2 of 2</p></center>
  </div>
</div>
{% endblock content %}

请帮助我

谢谢

PS:不确定问题的标题是否与内容相关。我想不出这个问题的恰当标题。抱歉。

0 个答案:

没有答案