Giter Club home page Giter Club logo

djangoexportcsv's Introduction

Django Export To CSV

Nesse tutorial vamos exportar os dados de uma tabela para CSV.

Documentação: https://docs.python.org/3/library/csv.html

myapp/views.py

import csv
import datetime
from django.http import HttpResponse

def export_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=Products' + \
        str(datetime.datetime.now())+'.csv'
    writer = csv.writer(response)
    writer.writerow(['Title1','Title2','Title3','Title4']) # head Titulo
    
    products = Product.objects.all() # lista todos os produtos
 
    for product in products:
        image_product = [el.image.url for el in product.products.all()] # todas as imagens do produto
        writer.writerow([product.name, product.price, 
            product.description,image_product])
    return response

myapp/templates/urls.py

from django.urls import path 
from myapp import views

urlpatterns = [
    path('', views.product_list, name='product-list'), 
    path('create-product/', views.form_product, name='product-create'),
    path('export-csv/', views.export_csv, name='export-csv'),  
]

myapp/templates/index.html

<a class="btn btn-success" href="{% url 'export-csv' %}">ExportCSV</a>

Django ExportCSV com Filtro

import csv
import datetime
from django.http import HttpResponse

def export_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=Products' + \
        str(datetime.datetime.now())+'.csv'
    writer = csv.writer(response)
    writer.writerow(['Title1','Title2','Title3','Title4']) # head Titulo
    
    # products = Product.objects.all() # lista todos os produtos
		obj = request.GET.get('obj')
    # products = Product.objects.filter(name__icontains=obj) # lista todos os produtos 
    print(obj) 
    if obj:  
        products = Product.objects.filter(name__icontains=obj)  
    else:
        products = Product.objects.all()

    for product in products:
        image_product = [el.image.url for el in product.products.all()] # todas as imagens do produto
        writer.writerow([product.name, product.price, 
            product.description,image_product])
    return response
<a class="btn btn-success" href="{% url 'export-csv' %}?obj={{request.GET.obj}}">ExportCSV</a>

djangoexportcsv's People

Contributors

eticialima avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.