0

I am trying to develop a button that uploads a CSV that has some words in it to a DBtable through a button, the problem is that when I click the button nothing happens, I don't know if I'm missing something or I have to do it some other way. Thank you

views.py:

def uploadWords(request):
    up = request.POST.get('Upload') 
    if up == "Upload":
        if request.user.is_authenticated:
            form = UploadFileForm()
            if request.method == "POST":
                 form = UploadFileForm(request.POST, request.FILES)
                 if form.is_valid():
                     file = request.FILES['file']
                     usr = User.objects.get(username=request.user)
                     if file.name.endswith(".csv"):
                        reader = csv.reader(file)
                        for row in reader:
                           wt = WordsTable()
                           wt.user = usr
                           wt.word1 = row[0]
                           wt.word2 = row[1]
                           wt.word3 = row[2]
                           wt.word4 = row[3]
                           wt.word5 = row[4]
                           wt.save()
                        messages.success(request, "File uploaded successfully")
                       return redirect("home")
                else:
                    messages.info(request, "File is not csv")
                    return redirect("home")
        context = {'form': form}
        return render(request, "base.html", context)
    else:
        return redirect("index")

urls.py:

urlpatterns = [
path('', views.index, name='index'),
path('home/', views.home, name='home'),
path('login/', views.loginView, name='login'),
path('logout/', views.logoutUser, name='logout'),
path('register/', views.register, name='register'),
path('upload/', views.uploadWords, name='upload'),
] 

base.html:

<div style="text-align:center;display:block;">
   <form method="POST" enctype="multipart/form-data">
     {% csrf_token %}
     <input type="file" name="upload" accept=".csv, .xlsx">
     <br>
     <button class="button btn btn-primary" type="submit" name="up" value="Upload">Upload</button>
   </form>
</div>

3
  • 1
    Try adding in form tag action="{% url 'upload' %}" Commented Mar 28, 2022 at 9:44
  • Yeah, if I do that I get: The view NLPApp.views.uploadWords didn't return an HttpResponse object. It returned None instead. Do you know why? maybe the view is wrong Commented Mar 28, 2022 at 10:47
  • Yep, it looks like there is something wrong with view, but I'm not sure where you missing return Commented Mar 28, 2022 at 11:30

1 Answer 1

1

the name of the button is "up", so you need to change

up = request.POST.get('up') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.