Tuesday, 22 October 2019

Write a Django based web server to parse a user request (POST), and write it to a CSV file


Programming steps(Reference)


1. O.S windows 64bit
2. Internet connection compulsory
3. python 64 bit

4. open cmd prompt
  pip install django

5. open windows drive d
   create folder dj

6. open cmd prompt
   d:
   cd dj
   django-admin startproject KVS     #open windows drive d and dj folder check KVS folder creation
   cd KVS                                           # inner folder and outer folder and it creates python files
   manage.py runserver
   copy the starting development server ip address: http://127.0.0.1:800/ and paste in the webpage
   In the command prompt press ctrl+c
   manage.py startapp teacher

7. open windows drive d wid Django folder
   -open  outer and inner KVS folder
    - open setting file with IDLE
     coding
            installed_apps add teacher at the last
            go to the template and include in dirs template

8. open note pad and type html code in the file first.html and save it in the folder
D:\Django\KVS\temp    # Get method
<html>
 <head><title>Welcome to KVS</title></head>
  <body bgcolor=skyblue>
      <h1> This is first Django Project.</h1>
           {{even}}
     
        {% for a in even %}
          {{a}}
           <br>
        {% endfor %}
 
    {{name}}
 
    <a href="http://127.0.0.1:8000/stuentry">
     <h1>Click Here to add new student.</h1>
     </a>
  </body>
</html>

9. open note pad and type html code in the file stuentry.html and save it in the folder
D:\dj\KVS\temp  # post method
<html>
 <head><title>Student entry form</title></head>
  <body bgcolor=skyblue>
     <h1>Enter the details</h1>

     <form action="#" method="POST">
         {%csrf_token%}
         Roll: &nbsp <input type="text" name="roll no"> <br><br>
         Name: &nbsp <input type="text" name="name"> <br><br>
         <input type="submit"> <br><br>
      </form>
      <a href="http://127.0.0.1:8000/first">
         <h1>Click Here to view th details of students.
         </h1>
      </a1>
   </body>
</html>

10.create excel and save it as "stud.csv" and put some entry
e.g.,
roll,name
1,Hari
2,Mukesh

11.In dj\KVS, go to teachers folder and edit views.py in IDLE
from django.shortcuts import render
import pandas as pd
from django.http import HttpResponseRedirect
import csv

# Create your views here.
 def show(request):
     lst=[x for x in range(2,20,2)]
     df=pd.read_csv("D:\\dj\\stud.csv")
     return render(request,'first.html',{"even":lst,"name":df})
 def stuentry(request,):
     if request.method=='POST':
         stu_dict=request.POST
         with open("D:\\dj\\stud.csv","a") as st:
             Val=csv.writer(st)
             L=[]
             for x in stu_dict:
                 if x=="rollno":
                     L.append(stu_dict[x])
                 if x=="name":
                     L.append(stu_dict[x])
             Val.writerow(L)
     return render(request,'stuentry.html')

12.Now go to KVS folder inside of KVS. Edit urls.py with IDLE
from django.contrib import admin
from django.urls import path
from teacher import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('first',view.show),
    path('stuentry',views.stuentry),
    ]

13.open cmd prompt
   -manage.py runserver
   -copy server address (e.g., http://127.0.0.1:8000/)

14.open web browser and paste the address with "first" (i.e., http://127.0.0.1:8000/first)

Now you have your web with GET and POST method














































































No comments:

Post a Comment