SoFunction
Updated on 2024-10-29

Django form submission after the realization of the same name to get different value value

i Front-end: nput_test.html

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>Search</title>
  <script>
 
  </script>
</head>
<body>
  <div><p>{{ result }}</p></div>
  <form action="" method="post">{% csrf_token %}
    <input type="text" name="key">
    <input type="text" name="key">
    <input type="submit" value="Search" >
  </form>
</body>
</html>

Both <input > tags name are 'key'

Backend: Django

def setting(request):
  if  == 'POST':
    result_list = ('key', '')
    result = str(result_list)
    return render_to_response('input_test.html', locals(), context_instance=RequestContext(request))
  else:
    return render_to_response('input_test.html', locals(), context_instance=RequestContext(request))

After you run it, enter it in the browser form:

Open chrome, submit the form, see FormData, in fact, the browser has submitted to the background of the two name for the 'key ' value.


The backend also successfully received a list ['1', '2'].

The main thing is that this API (), which receives all values that share the same name.

(key, default)¶

Returns the data with the requested key, as a Python list. Returns an empty list if the key doesn't exist and no default value was provided.


It's guaranteed to return a list of some sort unless the default value was no list.

Reference:/questions/14348321/getting-distinct-values-from-inputs-that-share-the-same-name

Additional knowledge:django form from the back-end query display back to the front-end and form submission to the server operation

This table is the effect of my following code appears, the following code can also be realized in the figure to modify the save delete operation.

1: First you have to create a table, where the table statement I will no longer write, no longer simple. (Do not forget to add some data Oh!)

2: Return the table data to the front-end through the function. Here stu is the name of the table, and all the returned results all are passed to the front-end all through a data dictionary

def showstu(request):
  all=()
  data={
    "all":all
  }

return render(request,"",context=data)

3: There's data we can get to the front end to display.

4: create a new file, used to show with.

This is the simplest form to use for hints.

 <table border="1" cellpadding="0" >
      <tr >

      <td>name and surname</td>
      <td>(a person's) age</td>
      <td>distinguishing between the sexes</td>
      <td>classes or grades in school</td>
      <td colspan="3">manipulate</td>
      </tr>
</table>

This table is the table that implements our function

{% for student in all %}

<form action="{% url 'homework:updatestu' %}" method="get" >
<table border="1" cellpadding="0">
<tr><td>
 <input type="text" name="s_id" value={{}} style="display:none">
</td><td>
  <input type="text" name="s_name" value={{student.stu_name}} >
</td><td>
  <input type="text" name="s_age"value={{ student.stu_age}}>
</td><td>
  <input type="text" name="s_sex"value={{ student.stu_sex}}>
</td><td>
  <input type="text" name="s_cla" value={{ student.stu_class}}>
</td><td>
    <input type="button"  value="Modification" οnclick="update(this)">

# Here two methods are used to submit to the server, a submit submission and a hyperlink submission.
</td><td><input type="submit" value="Save." οnclick="save(this)">
</td><td><a href="/homework/delstu/{{  }}" rel="external nofollow" >
   <input type="button" value="Delete"></a>
</td></tr>
    </table>
    </form>

{% endfor %}

Well the query function is now ready we can access it via url!

url(r"^showstu/", ,name="showstu"),

Now up to operate the rest of the function, here's a rather stupid way to realize that clicking on the corresponding button to delete or modify which one, you have to set up each line as a form to submit the form.

So I put the form inside the for loop so that looping one line results in one more form.

5: Implementation of the back-end processing function after the submission of the back-end, via get.

Update Operation

def updatestu(request):
  id = ("s_id")
  name=("s_name")
  sex=("s_sex")
  age=("s_age")
  cla=("s_cla")

# Get the id in the database and set the data related to this id to the value you got before, don't forget to save! Refresh the page directly after modification, that is, redirect to this page.

  stu1=(pk=id).first()
  stu1.stu_age=age
  stu1.stu_class=cla
  stu1.stu_sex=sex
  stu1.stu_name=name
  ()
  return HttpResponseRedirect("/homework/showstu")

Delete operation

def delstu(request,id):
  stu1=(pk=id).first()
  ()
  return HttpResponseRedirect("/homework/showstu")

Above this Django form submitted after the realization of getting the same name of the different value value is what I share with you all, I hope to give you a reference, but also hope that you support me more.