6

I'm trying get my form POST data onto next page but getting the error

You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/robustSearch/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

my urls.py file

from django.urls import path
from . import views


urlpatterns = [
    path('', views.home, name='home'),
    path('search_titles', views.searchTitles, name='search_titles'),
    path('stats/', views.dataStats, name='stats'),

    path('robustSearch/', views.robustSearch, name='robustSearch'),
]

And my views.py file

def robustSearch(request):
    
    if request.method == 'POST':
        file = request.FILES['titles_file']
        df = pd.read_csv(file)
        df.dropna(inplace=True)
        counting = df.counts()
    context={
        'counting': counting,
    }
    return render(request, 'result_titles.html', context)

and my POST Form file is

<form action="robustSearch" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-inline">
     <input type="file" name="titles_file" class="form-control input-sm mr-2">
     <button type="submit" class="btn btn-primary">Search</button>
</div>  
</form>

anyone can please point out where I'm doing wrong or how can I get this purpose fulfilled

Tabraiz Yasin
  • 65
  • 1
  • 5

2 Answers2

4

The URL should be:

<form action="/robustSearch/" method="POST" enctype="multipart/form-data">
  …
</form>

but it is better to work with the {% url … %} template tag [Django-doc]:

<form action="{% url 'robustSearch' %}" method="POST" enctype="multipart/form-data">
  …
</form>
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

you lack "/" in your link; this causes that error

in ur urls

path('robustSearch/', views.robustSearch, name='robustSearch'),

and in ur html

action='robustSearch'

it should be

action='robustSearch/'

or you can consult django - url with automatic slash adding

lam vu Nguyen
  • 433
  • 4
  • 9