Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django视图未执行_Django_Django Views_Django Templates - Fatal编程技术网

Django视图未执行

Django视图未执行,django,django-views,django-templates,Django,Django Views,Django Templates,我正在django的一个应用程序中实现excel导出功能,但是当我单击应该执行视图的链接时,什么都没有发生,服务器根本没有运行视图 这是应该运行该视图的按钮: <li class="nav-item mt-sm-2 mt-lg-0"> <a href="{% url 'catalog:export_all_csv' %}" class="btn btn-success btn-sm" download=&quo

我正在django的一个应用程序中实现excel导出功能,但是当我单击应该执行视图的链接时,什么都没有发生,服务器根本没有运行视图

这是应该运行该视图的按钮:

<li class="nav-item mt-sm-2 mt-lg-0">
    <a href="{% url 'catalog:export_all_csv' %}" class="btn btn-success btn-sm" download="OrdenesProximas" tabindex="0" data-toggle="popover" data-trigger="focus" title="¡Archivo descargado!" data-content="El archivo ha sido descargado exitosamente.">
        <i class="uil-file"></i>
            Exportar catálogo a Excel
    </a>
</li>

据我所知,url是正确的,因此它应该进入视图,在一个新选项卡中下载csv文件,但它只是重新加载页面,什么也不做。

因此,您的url
产品列表(按类别)
可能会被选为第一个匹配的url。Django URL的工作方式是执行第一个匹配的URL

通过更改URL的顺序,您的导出应该可以工作


app_name = 'catalog'

urlpatterns = [
    path('catalog/list/', views.ProductList.as_view(), name='product_list_table'),
    path('catalog/add/', views.create_product, name='add_product'),
    path('catalog/category/add/', views.CategoryCreate.as_view(), name='add_category'),
    path('catalog/export_csv/', views.export_csv, name='export_all_csv'),  # note this is before the slug capture.
    path('catalog/<slug:category_slug>/', views.product_list, name='product_list_by_category'),
    path('catalog/<slug>/list/', views.CategoryProductList.as_view(), name='category_product_list_table'),
    path('catalog/', views.product_list, name='product_list'),
    path('catalog/<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
    path('catalog/<int:id>/<slug:slug>/edit/', views.edit, name='product_edit'),
    
]

您能否提供一些关于项目结构的信息:您的按钮所在的视图、路径和html?您是否忘了
附件
response['Content-Disposition']=“attachment;filename='productos.csv'”
@albar我将其更正为您发送的内容,现在它正在导出页面的HTML而不是csv文件。我有另一个视图,在另一个应用程序中做完全相同的事情,但它工作得很好。@Razenstein你是什么意思?项目的其余部分工作正常。只有这一个视图无法运行。请确认没有触发视图
路径('catalog/',views.product\u list,name='product\u list\u by\u category'),
,而不是导出。我认为这个URL可能会作为第一个匹配的URL执行。
@login_required
def export_csv(request):
    import pdb; pdb.set_trace()
    categories = Category.objects.filter(company=request.user.profile.company)
    queryset = Product.objects.filter(category__in=categories)
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = f'filename=productos.csv'
    writer = csv.writer(response)
    writer.writerow(['Id de Producto', 'Categoría', 'Nombre del producto', 'SKU', 'Código de barras',
        'Marca', 'Proveedor', 'Color', 'Medidas', 'Descripción', 'Observaciones', 'Precio 1', 'Precio 2',
        'Precio 3', 'Impuesto (%)', 'Costo de fabricación'])
    for item in queryset:
        writer.writerow([item.id, item.category.name, item.name, item.sku, item.barcode, item.brand,
            item.provider, item.color, item.measures, item.description, item.observations, item.price_1,
            item.price_2, item.price_3, item.tax, item.fabrication_cost])
    return response

app_name = 'catalog'

urlpatterns = [
    path('catalog/list/', views.ProductList.as_view(), name='product_list_table'),
    path('catalog/add/', views.create_product, name='add_product'),
    path('catalog/category/add/', views.CategoryCreate.as_view(), name='add_category'),
    path('catalog/export_csv/', views.export_csv, name='export_all_csv'),  # note this is before the slug capture.
    path('catalog/<slug:category_slug>/', views.product_list, name='product_list_by_category'),
    path('catalog/<slug>/list/', views.CategoryProductList.as_view(), name='category_product_list_table'),
    path('catalog/', views.product_list, name='product_list'),
    path('catalog/<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
    path('catalog/<int:id>/<slug:slug>/edit/', views.edit, name='product_edit'),
    
]
---------------------------------------------------------------------------------------------------
| View function    | Arguments |Keyword arguments                              |URL name    |
---------------------------------------------------------------------------------------------------
|myapp.views.MyView| ()        | {'pk': '55959533-9df0-427f-a904-79207f2613a3'}|product_list|
---------------------------------------------------------------------------------------------------