pythondjangourl-patternform-post

how to solve Django POST URL error of APPEND_SLASH


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


Solution

  • 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>