Posts

How to Install Express Application

  1. Install Express Generator D:\myproject > npm install -g express-generator 2. Install Express Application npx express --view=ejs MyFirstApp 3. Install Dependencies PS D:\myproject > cd MyFirstApp PS D:\myproject > MyFirstApp>npm install 4. Run Express Application npm start http: //localhost:3000/

How to solve Server Error (500) when I set Debug - False (Django Heroku )

  Here are the steps that worked for me: step 0:  In your command line, go to your project directory (where manage.py exists) Step 1: pip install whitenoise      make sure this library is added  to the requirements.txt ( pip freeze > requirements.txt) Step 2:   go to your settings.py folder and locate MIDDLEWARE. add 'whitenoise.middleware.WhiteNoiseMiddleware' ,   Step 3: in the same settings.py add STATIC_ROOT = os.path.join(BASE_DIR, 'static') for my case it was just above django_heroku.settings(locals()) Step 4: now go to your wsgi.py file in the same directory as settings.py type the following at the top from whitenoise import WhiteNoise also at the bottom you will use the import so add application = WhiteNoise(application)   Step 5:  now change DEBUG = False and go back to the command prompt in the project directory Step 6: type python manage.py collectstatic after this a new folder will appear in the root directory of your project . Just leave it there

saving and dwnloading excel file in django

  import pandas as pd import os import openpyxl import xlwt def Category1_excel ( request ):      #saving file  category1 =Download . objects . all () file =[] for i in category1 : p = i .column1 s = i .column2 q = i .column3 row =[ p , s , q , e , l ] file . append ( row ) document = pd . DataFrame ( file ) document . to_excel ( 'media/file_downloads/category1.xlsx' )           #downloading file      category1 =Download . objects . all () response = HttpResponse ( content_type = 'application/ms-excel' ) response [ 'Content-Disposition' ] = 'attachment; filename="file.xls"' wb = xlwt . Workbook ( encoding = 'utf-8' ) ws = wb . add_sheet ( "sheet1" ) font_style = xlwt . XFStyle () font_style . font . bold = True row_num = 0 ws .write( row_num , 0 , 'PURCHASED GOODS' , font_style ) ws .write( row_n

How to save and download a csv file in django

  import pandas as pd   def Category1_csv ( request ): category1 =Database . objects . all () file =[] for i in category1 : p = i .abc row =[ p ,... ] file . append ( row )      #to save a file document = pd . DataFrame ( file ) document . to_csv ( 'media/file_downloads/category1.csv' ) #return HttpResponse("file createrd")      # to download a file data = open ( os . path . join ( settings .BASE_DIR, 'media/file_downloads/category1.csv' ), 'r' ). read () resp = HttpResponse ( data ) resp [ 'Content-Disposition' ] = 'attachment;filename=csv file.csv' return resp  

how to install python library manually

  Download the package unzip it if it is zipped cd  into the directory containing setup.py If there are any installation instructions contained in documentation, read and follow the instructions OTHERWISE type in  python setup.py install

Exporting HTML Page into PDF In Django

Views.py from django.shortcuts import render from io import BytesIO from django.http import HttpResponse from django.template.loader import get_template from xhtml2pdf import pisa   from django.views import generic import pandas as pd # Create your views here. # defining the function to convert an HTML file to a PDF file def html_to_pdf ( template_src , context_dict ={}):      template = get_template(template_src)      html  = template.render(context_dict)      result = BytesIO()      pdf = pisa.pisaDocument(BytesIO(html.encode( "ISO-8859-1" )), result)       if not pdf.err:           return HttpResponse(result.getvalue(), content_type = 'application/pdf' )       return None class GeneratePdf ( generic . View ):       def get ( self , request , * args , ** kwargs ):                   # getting the template         pdf = html_to_pdf( 'app/dashboard/climate_risk.html' )                     # rendering the template         return HttpRespons