ALB ALB - 1 year ago 34
HTML Question

Accessing a string in a link's text

I have a large html page of images which are individually linked to an html form. The action of the form then points to a Django script which processes key-value pairs and writes to a yaml file. All of these images have an 'image id' which is readily accessible from the html of the link (viz. I have a bunch of

<a href .../html_form><img src=longurl.../image_id /></a href .../html_form.>

lines generating the page). I need some way to get the string "image_id" as one of the key-value pairs ('ID_of_image' : image_id) posted by the form to the django script and thus written to the yaml file. It was suggested that I some how modify the action of the form from

<form name=form_name action="serverpath/django_script_function" method="post">


<form name=form_name action="serverpath/django_script_function/image_id" method="post">

which, from what I can tell by googling, I should use some way of dynamically generating html to do. My questions are the following:

(i) how do I access the string image_id in the form or script to dynamically generate the form?

(ii) the only modification to the form will be the action above; do I really need to use something to dynamically generate html to do this?

Answer Source

As I see, you are seeking a way to pass parameters from the anchor element to the form. There are a number of ways to achieve this.

For example, you can include the image id as a GET parameter of the form; when generating the form, insert it as a value of a hidden input:

<!-- image link template -->
<a href=".../html_form?image_id={{img_id}}"><img src=".../imgs/{{img_id}}"></a> 

<!-- form template; the img_id variable should be accessible from the request 
object of your view, like request["image_id"] -->
<form name="form_name" action="serverpath/django_script_function">
    <input name="image_id" type="hidden" value="{{img_id}}"></input>
    <!-- other form fields.. -->