简体   繁体   中英

how to send an post request?

i am making a website only for learning purpose. When i try to use post request, it shows me the following error:

RuntimeError at /analyse

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/analyse/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

Here is my views.py and index.html:

views.py:

refer to djtext = request.post.get('text', 'default')

# I have created this file - Piyush
from django.http import HttpResponse
from django.shortcuts import render


def index(request):
return render(request, 'index.html')

# return HttpResponse("Home")


def analyse(request):
global line
djtext = request.POST.get('text', 'default')
print(djtext)

# Check checkbox value
removepunc = request.GET.get('removepunc', 'off')
fullcaps = request.GET.get('fullcaps', 'off')
newlineremover = request.GET.get('newlineremover', 'off')
extraspaceremover = request.GET.get('extraspaceremover', 'off')


    # Check which checkbox is on
    if removepunc == "on":

    punctuations = '''!()-{}[]:;'",<>./?@#$%^&*_~'''
    analysed = ""
    for char in djtext:
        if char not in punctuations:
            analysed = analysed + char

    params = {'purpose': 'Removed punctuations', 'analysed_text': analysed}
    return render(request, 'analyse.html', params)
    elif fullcaps == "on":
    analysed = ""
    for char in djtext:
        analysed = analysed + char.upper()

    params = {'purpose': 'Change to UPPERCASE', 'analysed_text': analysed}
    return render(request, 'analyse.html', params)

    elif newlineremover == "on":
    analysed = ""
    for char in djtext:
        if char != "\n":
            analysed = analysed + char

    params = {'purpose': 'Removed new Lines', 'analysed_text': analysed}
    return render(request, 'analyse.html', params)

    elif extraspaceremover == "on":
    analysed = ""
    for index, char in enumerate(djtext):
        if djtext[index] == " " and djtext[index + 1] == " ":
            pass
        else:
            analysed = analysed + char
    params = {'purpose': 'Remove extra space', 'analysed_text': analysed}
    return render(request, 'analyse.html', params)

    else:
    return HttpResponse("Error")

# def capfirst(request):
#     return HttpResponse("capitalise first <a href='/'>back</a>")
#
#
# def newlineremover(request):
#     return HttpResponse("new line <a href='/'>back</a>")
#
#
# def spaceremove(request):
#     return HttpResponse("space remover <a href='/'>back</a>")
#
#
# def charcount(request):
#     return HttpResponse("charcount <a href='/'>back</a>")

index.html:

refer to <div class="container"><form action="/analyse" method='post'>

<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
  <head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" 
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9 
aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<title>TextUtils.com</title>
 </head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Welcome to TextUtils</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data- 
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria- 
label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>

   <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
      <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">About Us</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Contact Us</a>
  </li>
</ul>
<form class="form-inline my-2 my-lg-0">
  <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
</div>
</nav>

<div class="alert alert-success alert-dismissible fade show" role="alert">
  <strong>Welcome to textutils</strong> Change your Text with your taste!
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button>
</div>

<div class="container">
  <form action="/analyse" method='post'>


  <div class="form-group">
    <label for="exampleFormControlTextarea1">Write your text here...</label>
    <textarea class="form-control" name="text" id="exampleFormControlTextarea1" rows="5"></textarea>
  </div>
<div class="custom-control custom-switch">
  <input type="checkbox"name="removepunc" class="custom-control-input" id="customswitch1">
  <label class="custom-control-label" for="customswitch1">Remove Punctuations</label>
</div>

  <div class="custom-control custom-switch">
  <input type="checkbox"name="fullcaps" class="custom-control-input" id="customswitch2">
  <label class="custom-control-label" for="customswitch2">Uppercase</label>
</div>

  <div class="custom-control custom-switch">
  <input type="checkbox"name="newlineremover" class="custom-control-input" id="customswitch3">
  <label class="custom-control-label" for="customswitch3">New Line Remover</label>
</div>

  <div class="custom-control custom-switch">
  <input type="checkbox"name="extraspaceremover" class="custom-control-input" id="customswitch4">
  <label class="custom-control-label" for="customswitch4">Extra Spaces Remover</label>
</div>
<button type="Submit" class="btn btn-outline-dark mt-2">Analyse Text</button>
  </form>
</div>


<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384- 
DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384- 
Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" 
integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" 
crossorigin="anonymous"></script>
</body>
</html>

Make sure when you send any request it is followed by a / . Django will redirect you to the correct place with or without the slash but POST data is dropped on the redirect.

See this post for more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM