KEMBAR78
Django Step by Step Procedure | PDF | Comma Separated Values | Software
0% found this document useful (0 votes)
14 views11 pages

Django Step by Step Procedure

This document provides a step-by-step guide for setting up a Django project that handles CSV files, including creating a virtual environment, installing Django, and defining views for handling form data and CSV interactions. It also outlines how to set up a default SQLite database and switch to a MySQL database, detailing the necessary changes in the settings and models. Key functionalities include rendering HTML templates, processing form submissions, and reading/writing data to CSV or database tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

Django Step by Step Procedure

This document provides a step-by-step guide for setting up a Django project that handles CSV files, including creating a virtual environment, installing Django, and defining views for handling form data and CSV interactions. It also outlines how to set up a default SQLite database and switch to a MySQL database, detailing the necessary changes in the settings and models. Key functionalities include rendering HTML templates, processing form submissions, and reading/writing data to CSV or database tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DJANGO STEP BY STEP PROCEDURE for CSV

 Create a folder in any place on your desktop. Eg: Project in D Drive


 Open command prompt and enter the drive name where you created the folder and
then type cd folder name of your folder.
Eg: D:
Then type cd Project
 Now type pip install virtualenv
 Then type virtualenv venv(venv is the variable name we will use for virtual
environment)
Check the folder Project it should have a folder venv

 Now activate virtual environment by typing venv\Scripts\activate


 Then install Django type pip install Django
 Once the Django is install start your project:
Django-admin startproject student(student is the name I have given to the project)
You can check that in folder Project there will be another folder student
 Now type cd student
 Now check whether Django is installed successfully by typing:
python manage.py runserver
Open web browser and type localhost:8000
It will show Django installed successfully.

 After this start an app in your project


Django-admin startapp register(register is the name of our application)
 Now create a folder called templates in your main folder of student where we will
put our all html pages.
 Now open the settings file in student folder

And in installed apps add your app name

And add templates in templates section DIRS:

 Now design the HTML Pages in templates folder


Main.html

NOTE
 {%csrf_token%}----- important to link to views.
 A href=”{%url ‘second’%}” it gives a link and % url means url.py file which is
there in student folder and second is the view name there.
 Be careful with name=”name” and name=”empid” as this name and empid
will be used in views.py file in register folder to get data from this HTML
page.
Second.html
NOTE

 {% for i,j in details.items %}----- this will pick key and values from the dictionary
named details which is there in our views.py file in register folder
 <p>Employee Name:{{i}}</p>----Employee name will be key
 <p>Employee Id:{{j}}</p>---------Id will be value

1. Next step is to go to view.py file in Register folder where we will define the
functionality related to each page in terms of views(functions)

Views.py
first view: It is linked to main.html.
 data=request.POST will fetch the data from the main.html page
 dic[‘name’]=data[‘name’] . dic[‘name’] this name is variable name we are
giving to store data in dictionary. Data[‘name’] is coming from html page
main.html where we defined Name:<input type="text" name="name">
 dic[‘empid’]=data[‘empid’]. dic[‘empid’] is the name we give to store
employee id in dictionary and data[‘empid’] is coming from main.html where
we defined Employee Id:<input type="text" name="empid">
 fieldnames=('name','empid') this is column header as we don’t want to write
this in csv data
 with open("empdetails.csv",'a')as ecsv: open a csv file empdetails.csv
 writer=csv.DictWriter(ecsv,fieldnames=fieldnames) declare a variable writer
which we will use to write data in csv file
 writer.writerow(dic) we write the data of dictionary in csv file
 return render(request,'main.html') this will make browser stay on same page
main.html after submitting the data
 else:
 return render(request,'main.html') this will display the main.html page
even if we don’t click on submit button
This is the second view related to page second.html

 with open("empdetails.csv",'r') as ecsv: open the csv file to read data


 reader=csv.reader(ecsv) declare a variable reader to use csv reader
 for line in reader: for each line in csv file
 if line==[]:
 Continue if line is blank in csv file then continue
 else:
 dic[line[0]]=line[1] make the first value of csv file(name) as key and second
value in csv file(empid) as value
 return render(request,'second.html',{'details':dic}) return second.html page in
browser where we pass the dictionary with named as details to second.html so that
the browser can fetch details form csv file as dictionary

2. Now open urls.py file in student folder


Make these changes in urls.py

 from register import views---import vies.py form register folder


 add path for first view and second view. path('second/',views.second,
name='second'), used in main.html <a href="{%url 'second'%}">CHECK
DETAILS</a>
DJANGO STEP BY STEP PROCEDURE for default database sqlite

Step 1 to 12 remains same


In Step 12 add name of database as employee in databases section of settings.py

Now go to models.py in register and define your database tables:

 emp is table name in database employee


 empid and name are column names

In Step 13 main.html remains same change second.html

 {% for employee in employees%} employees is dictionary passed from


viewe.py. So we traverse each row of employees
 Employee Id:{{employee.empid}}<br> column name of database
 Employee Name:{{employee.name}}<br> column name from database
In view.py
First view register

 ename=data['name'] and empid=data['empid'] get data from html page and save it
in variables ename and empid
 newemp=emp(name=ename,empid=empid) create a new object saying table emp
column name is ename variable and column empid is empid variable.
 newemp.save() save the object in database
 return redirect('second') go to second page after clicking submit
details view

 employees=emp.objects.all() get all the data from emp table as bjects and save in
dictionary employee
 return render(request,'second.html',{'employees':employees}) pass the dictionary
to second.html by name employees.
Url file stays as it is
DJANGO STEP BY STEP PROCEDURE for MYSQL as database
In command prompt type pip install mysqlclient
Everything else remains same as sqlite only change setting.py database section

You might also like