0

I'd like to use this at my homepage in django project(python 3.7.2 and django 2.1.7 installed). Input file will be uploaded different way. I only want to know how to use/where to paste script like this. Thanks for help!

 from csv import reader, writer
    name = input("Please input your file's name.")
    with open(name) as file:
        csv_reader = reader(file)
        search_input = input("Search by word?")        
        for search in csv_reader:
            for rows in search:
                if search_input == rows:
                    print(search)

1 Answer 1

1

You reading it from local memory you can grab a file from request it will be in memory then you can pass in memory file to csv.reader it can read it.

You should create a form first like this or you can use django-form in template:

<form action="/your/url/here" method="POST" enctype="multipart/form-data">
   <input name="uploadedfile" type="file" />
<br>
 <label>Word</label>
    <input name="word" type="text" />
    <br>

    <input type="submit" value="Upload">
</form>

In your view you can do

import csv
def myview(request):
    csv_reader = csv.reader(request.FILES['uploadedfile']) #to read in memory file
    search=request.POST.get("word")
    #your stuff here
    for search in csv_reader:
            for rows in search:
                if search_input == rows:
                    #you can return HttpResponse
                    return HttpRespose(search)
    #or return something like this if not found
    return HttpResponse("Not found")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.