Commit fb96024d authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

upload files using upload/ done

parent 784d2f15
...@@ -13,10 +13,13 @@ Including another URLconf ...@@ -13,10 +13,13 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from quiz.views import upload_file
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path
from quiz import views as a from quiz import views as a
from django.conf import settings from django.conf import settings
from django.conf.urls import url
from django.views.generic import TemplateView
from django.conf.urls.static import static from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
...@@ -25,4 +28,7 @@ urlpatterns = [ ...@@ -25,4 +28,7 @@ urlpatterns = [
path('quiz/',a.index), path('quiz/',a.index),
path('save_ans/',a.save_ans,name="saveans"), path('save_ans/',a.save_ans,name="saveans"),
path('result/',a.result,name="result"), path('result/',a.result,name="result"),
url(r'^upload/',TemplateView.as_view(template_name = 'upload.html')),
path('uploaded/',a.upload_file,name="uploaded")
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) ]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
No preview for this file type
question1,option1,option2,option3,option4,option1,type,10,5,explaination,6969
question1,option1,option2,option3,option4,option1,type,10,5,explaination,6969
from django import forms
class UploadFileForm(forms.Form):
file = forms.FileField()
\ No newline at end of file
import csv
from prettytable import PrettyTable
def readCSV(file):
rows=list()
reader=csv.reader(file)
for row in reader:
#insert the questions into sql database
rows.append(tuple(i for i in row))
return rows
def printTable(rows):
x = PrettyTable()
for row in rows:
x.add_row(row)
print(x)
#f =open("file.csv",'r')
#rows=readCSV(f)
#printTable(rows)
<html>
<body>
<form name = "form" enctype = "multipart/form-data"
action = "{% url "uploaded" %}" method = "POST" >{% csrf_token %}
<br>
<div style = "max-width:470px;">
<center>
<input type = "file" style = "margin-left:20%;"
placeholder = "file" name = "file" id= />
</center>
</div>
<br>
<div style = "max-width:470px;">
<center>
<button style = "border:0px;background-color:#4285F4; margin-top:8%;
height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
<strong>Login</strong>
</button>
</center>
</div>
</form>
</body>
</html>
\ No newline at end of file
<html>
<body>
{% if uploaded %}
<strong>Your Quiz was uploaded.</strong>
{% endif %}
{% if not uploaded %}
{{temp}}
<strong>Your Quiz was not uploaded.</strong>
{% endif %}
</body>
</html>
\ No newline at end of file
...@@ -2,6 +2,10 @@ from django.shortcuts import render ...@@ -2,6 +2,10 @@ from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from . models import Questions from . models import Questions
from django.core.paginator import Paginator from django.core.paginator import Paginator
from .forms import UploadFileForm
from django.http import HttpResponseRedirect
from . newQuiz import readCSV
import csv
lst = [] lst = []
answers = Questions.objects.all() answers = Questions.objects.all()
...@@ -10,6 +14,7 @@ anslist = [] ...@@ -10,6 +14,7 @@ anslist = []
marksList=[] marksList=[]
negative=[] negative=[]
for i in answers: for i in answers:
print(str(i.answer)+" "+str(i.negative)+" "+str(i.marks))
anslist.append(i.answer) anslist.append(i.answer)
marksList.append(i.marks) marksList.append(i.marks)
negative.append(i.negative) negative.append(i.negative)
...@@ -35,8 +40,9 @@ def index(request): ...@@ -35,8 +40,9 @@ def index(request):
''' '''
def result(request): def result(request):
score =0 score =0
#print("lstlstlst"+str(len(anslist)))
for i in range(len(lst)): for i in range(len(lst)):
if lst[i]==anslist[i]: if lst[i]==anslist[i]:
score +=marksList[i] score +=marksList[i]
else: else:
score-=negative[i] score-=negative[i]
...@@ -47,7 +53,37 @@ def save_ans(request): ...@@ -47,7 +53,37 @@ def save_ans(request):
ans = request.GET['ans'] ans = request.GET['ans']
lst.append(ans) lst.append(ans)
def upload(request):
return render(request,'upload.html')
def student(request): def student(request):
lst.clear() lst.clear()
return render(request,'student.html') return render(request,'student.html')
def upload_file(request):
uploaded=False
temp1='test'
form = UploadFileForm(request.POST, request.FILES)
if request.method == 'POST' and form.is_valid():
handle_uploaded_file(request.FILES['file'])
uploaded=True
return HttpResponse('success')
else :
return render(request,'upload.html',{temp1:'invalid File'})
def handle_uploaded_file(f):
with open('name.csv', 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
destination=open("name.csv","r")
rows=readCSV(destination)
for row in rows:
print(str(row))
q=Questions(question=row[0],option1=row[1],option2=row[2],option3=row[3],option4=row[4],answer=row[5],type=row[6],marks=int(row[7]),negative=float(row[8]),explainations=row[9],quizId=row[10])
q.save()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment