excel Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Mon, 18 Jan 2021 15:45:44 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 excel Archives - Tech Insights 32 32 230003556 How to Export Data from the Database into Excel using Django Python https://reactconf.org/export-data-from-database-into-excel-using-django/ https://reactconf.org/export-data-from-database-into-excel-using-django/#respond Mon, 18 Jan 2021 15:45:44 +0000 http://labpys.com/?p=274 In this tutorial, we will learn how to Export Data from the Database into Excel using Django. In this Django application, I used the pandas library. Let’s start Install  pandas …

The post How to Export Data from the Database into Excel using Django Python appeared first on Tech Insights.

]]>
In this tutorial, we will learn how to Export Data from the Database into Excel using Django. In this Django application, I used the pandas library.

Let’s start

Install  pandas library

First, create a Django project, then create models

Django Export Data From Database into Excel

models.py

from django.db import models
from Django import forms 

class tbl_Employee(models.Model):    
  #  Id = models.IntegerField()
    Empcode = models.CharField(max_length=10, default='')
    firstName = models.CharField(max_length=150,null=True)
    middleName = models.CharField(max_length=100,null=True)    
    lastName = models.CharField(max_length=100,null=True)
    email = models.CharField(max_length=30,null=True)
    phoneNo = models.CharField(max_length=12, default='',null=True)
    address = models.CharField(max_length=500, default='',null=True) 
    exprience = models.CharField(max_length=50, default='',null=True)        
    DOB = models.DateField(null=True, blank=True)   
    gender = models.CharField(max_length=10, default='',null=True)
    qualification = models.CharField(max_length=50,default='',null=True)   
    
     

    def __str__(self):
        return self.firstName
                
    objects = models.Manager()

let's migrate model
python manage.py makemigrations
python manage.py migrate
 

forms.py

 
from django import forms

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, Row, Column, Field

from .models import  tbl_Employee

class EmployeeRegistration(forms.ModelForm):
    class Meta:
        model = tbl_Employee
        fields =[ 'Empcode','firstName','middleName','lastName','email','phoneNo' ,'address','exprience',
                  'DOB','gender','qualification'
        ] 

Then let’s write a code in views.py to create a function to Export data from the database into Excel.

Views.py

from .models import tbl_Employee
import datetime as dt
import pandas as pd
import os
from django.conf import settings
from django.core.files.storage import FileSystemStorage

import csv
 
def export_users_csv(request):
   
    
    if request.method == 'POST':
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="EmployeeData.csv"'         
        writer = csv.writer(response)
        writer.writerow(['Employee Detail'])       
                
        
        writer.writerow(['Employee Code','Employee Name','Relation Name','Last Name','gender','DOB','e-mail','Contact No' ,'Address' ,'exprience','Qualification'])

        users = tbl_Employee.objects.all().values_list('Empcode','firstName' , 'middleName' , 'lastName','gender','DOB','email','phoneNo' ,'address','exprience','qualification')
        
        for user in users:
            writer.writerow(user)
        return response

    return render(request, 'exportexcel.html')

 then add the path to  the urls file

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("",views.base,name="base"),
    path("user_login/",views.user_login,name="user_login"),

 

    path('export_users_csv/', views.export_users_csv,name="export_users_csv"),  
     
    
]

Create a template folder in the root directory or app directory and a create html file named exportexcel.html

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> 
{% block content %}
<div class="shadow-lg continer">
<form method="post" enctype="multipart/form-data">
    {% csrf_token %}

    <div class="row">
        <div class="col-md-6 col-xs-12">
          <div class="x_panel">
            <div class="x_title">
              <h2>Data Export</h2>
           
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
<div class="row">
              <div class="col-md-8 col-sm-12 col-xs-12 form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-6" for="name">Company<span class="required">*</span>
                </label>
               
              </div>
            </div>
              <button type="submit" class="btn btn-success" >Export</button>                            
                            </div>
                        </div>
                    </div>
                </div>
  </form>
  </div>
   
{% endblock %}
Then execute command 
Python manage.py runserver 



See More: Import Data from Excel into Database using Django

The post How to Export Data from the Database into Excel using Django Python appeared first on Tech Insights.

]]>
https://reactconf.org/export-data-from-database-into-excel-using-django/feed/ 0 321
Import Data from Excel into Database using Django https://reactconf.org/import-data-from-excel-into-database-using-django/ https://reactconf.org/import-data-from-excel-into-database-using-django/#comments Mon, 18 Jan 2021 15:32:42 +0000 http://labpys.com/?p=269 Spreadsheets are commonly used by businesses and organizations to collect and retain data in today’s digital age. However, as the amount of data increases, these spreadsheets can become challenging to …

The post Import Data from Excel into Database using Django appeared first on Tech Insights.

]]>
Spreadsheets are commonly used by businesses and organizations to collect and retain data in today’s digital age. However, as the amount of data increases, these spreadsheets can become challenging to manage. That is where databases come in. Importing data from an Excel spreadsheet into a database is simple with Django, a high-level Python web framework. In this tutorial, we’ll learn how to import data from Excel into a Django database.

Create Django project 

We need to create a Django project  Import_Excel

     Django-admin startproject  Import_Excel

Create Django App

Now then we will create a app  import_excel_db  to perform import data from excel into database.

python  manage.py startapp import_excel_db

Install pandas and Django-import-export Library

Pandas 
 pip install pandas
Django-import-export
 pip install Django-import-export

Configure  Application Settings

Configure application settings by registering the App_name into the settings.py in a specific location at INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'import_excel_db',
]

Create the Model

Now we need to create an employee model.

from django.db import models

# Create your models here.

class Employee(models.Model):       
    Empcode = models.CharField(max_length=10, default='')
    firstName = models.CharField(max_length=150,null=True)
    middleName = models.CharField(max_length=100,null=True)    
    lastName = models.CharField(max_length=100,null=True)
    email = models.CharField(max_length=30,null=True)
    phoneNo = models.CharField(max_length=12, default='',null=True)
    address = models.CharField(max_length=500, default='',null=True)     
    DOB = models.DateField(null=True, blank=True)   
    gender = models.CharField(max_length=5, default='',null=True)
    qualification = models.CharField(max_length=50,default='',null=True) 
    salary = models.FloatField(max_length=50,default='',null=True)   
    
    def __str__(self):
        return self.firstName
                
    objects = models.Manager()

Create the resource file

Import data from Excel into the database using the Django-import-export library and this library works with the concept of Resource. So we need to create a resources.py file in the app folder.

from import_export import resources
from .models import Employee

class EmployeeResource(resources.ModelResource):
    class Meta:
        model = Employee

Django File Upload

from http.client import HTTPResponse
from django.shortcuts import render 
import os
from django.core.files.storage import FileSystemStorage
 
 
# Create your views here.

def Import_Excel_pandas(request):
    
    if request.method == 'POST' and request.FILES['myfile']:      
        myfile = request. FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)    
        return render(request, 'Import_excel_db.html', {
            'uploaded_file_url': uploaded_file_url
        })   
    return render(request, 'Import_excel_db.html',{})

Create the views functions

views.py

Importing data from Excel into database using Pandas library

from http.client import HTTPResponse
from django.shortcuts import render
import pandas as pd
import os
from django.core.files.storage import FileSystemStorage
from .models import Employee

# Create your views here.

def Import_Excel_pandas(request):
    
    if request.method == 'POST' and request.FILES['myfile']:      
        myfile = request. FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)              
        empexceldata = pd.read_excel(filename)        
        dbframe = empexceldata
        for dbframe in dbframe.itertuples():
            obj = Employee.objects.create(Empcode=dbframe.Empcode,firstName=dbframe.firstName, middleName=dbframe.middleName,
                                            lastName=dbframe.lastName, email=dbframe.email, phoneNo=dbframe.phoneNo, address=dbframe.address,
                                            gender=dbframe.gender, DOB=dbframe.DOB,salary=dbframe.Salary )           
            obj.save()
        return render(request, 'Import_excel_db.html', {
            'uploaded_file_url': uploaded_file_url
        })   
    return render(request, 'Import_excel_db.html',{})

Importing data from excel into database using  import-export library

from tablib import Dataset
from .Resources import EmployeeResource

def Import_excel(request):
    if request.method == 'POST' :
        Employee =EmployeeResource()
        dataset = Dataset()
        new_employee = request.FILES['myfile']
        data_import = dataset.load(new_employee.read())
        result = EmployeeResource.import_data(dataset,dry_run=True)
        if not result.has_errors():
            EmployeeResource.import_data(dataset,dry_run=False)        
    return render(request, 'Import_excel_db.html',{})

Provide Routing (URL patterns)

We need to add import_excel_db.urls to project urls.py

Urls.py

from django.contrib import admin
from django.urls import path,include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('import_excel_db.urls')),
]

Create urls.py file inside import_exceldb app to write the URL pattern or providing routing for the application.

urls.py

from django.urls import path
from . import views  
from Import_Excel import settings
from django.conf.urls.static import static
urlpatterns =[
path("",views.Import_Excel_pandas,name="Import_Excel_pandas"),
path('Import_Excel_pandas/', views.Import_Excel_pandas,name="Import_Excel_pandas"), 
path('Import_excel',views.Import_excel,name="Import_excel"),
] 
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)

Static Root Configuring

import os
STATIC_ROOT =  os.path.join(BASE_DIR, "assets")

Create Template

Create a template folder inside import_exceldb app and create html file in the directory.

import_excel_db.html

{% block content %}
  <form method="POST" enctype="multipart/form-data">
    {% csrf_token %}

    <div class="row">
        <div class="col-md-6 col-xs-12">
          <div class="x_panel">
            <div class="x_title">
              <h2>Import Excel Data into Database</h2>           
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <div class="col-md-8 col-sm-12 col-xs-12 form-group">      
              </div>
                    <input type="file" name="myfile" class="form-control">
                             <button type="submit" class="btn btn-success" >Upload data using Pandas</button> 
                             <button type="submit" class="btn btn-success" >Upload data using Import_Export_Lib</button>   
                            </div>
                        </div>
                    </div>
                </div>
  </form>   
{% endblock %}
Now make migrations
 Python manage.py makemigrations
Python manage.py migrate
Python manage.py runserver
Document

More Related Posts



The post Import Data from Excel into Database using Django appeared first on Tech Insights.

]]>
https://reactconf.org/import-data-from-excel-into-database-using-django/feed/ 10 320