Is it possible for 2 POST request in one template?

Is it possible to do 2 POST request in a single template. I have a webpage that has a drop down box. This drop down box loops a database to display hostname (the field in my database). With the chosen hostname, further details are displayed regarding the hostname in another table to another drop down box for user to choose. Then there are textboxes to key for user to key in more detailed details. And finally there is a save button at the end. Or i have to make 2 different templates. Whereby the first one is to get the selected value then the next one for user to key in data?

Also, 1 question. I have a string i have used it to get the selected choice from my template. But it says device matching query does not exist. This the statement i used:
selecteddata = Device.objects.get(hostname='selected')
selected contains the string of TestTest

This my model for Device:

class Device(models.Model):
    hostname = models.CharField(max_length=50, unique = True)
    ipaddr = models.GenericIPAddressField(protocol='ipv4', unique=True, verbose_name='mangement IP') ##Use for mgt_id_addr
    date_added = models.DateTimeField(
    def __str__(self):
        return self.hostname

And in my table, there is the record of TestTest


Short answer is “Yes”, with some clarifications / qualifications.

Templates don’t do POST requests. Templates don’t do anything. Templates are what are used to help create the pages that are sent to the browser. All POST requests are performed by the browser.

Why is this distinction important? Because how a browser make a POST request matters. If the browser makes a POST request via an HTML form’s submit button, the browser is going to replace the current page with the response received from the server.

If you want to just update part of the page with some data retrieved from the server, then that POST request needs to be issued through JavaScript. The JavaScript then has the responsibility of updating the appropriate part of the page with the data received.

So, the technique you’re looking for is frequently referred to as “chained dropdowns” or “linked dropdowns” - a very commonly used technique. There are different ways of implementing it, usually depending upon what JavaScript frameworks you may be using and what the specific requirements are for your dependencies. But they all consist of two functional parts - the JavaScript to make requests, and views to respond to those requests with the appropriate data.

Thanks for the detail answer ! So that means in my case, I have to come out with the javascript to take the value of the dropdown box, connect to SQLite database, do comparison with a table to get id then using the id to get the correct table and return back the data?

Not quite. You write the JavaScript to issue a post request to a view. The view returns data, which the JavaScript then uses to update the page.

Oh okay. I shall go read up on the technique then

I watch some videos on it. It is done when 2 table is identified. But for my case, only 1 table is known at the moment. The other table will depend on the selected value which is looping thru the known table. I dont think i can do both within the same web though. It seems to me the only way is creating 2 template when the 1st template is choosing the value and passing a id to the 2nd template to retrieve the correct table and display. Am i correct?

No you are not correct.

Templates don’t choose and pass data to other templates.

All work is either done by views (only on the server) or within JavaScript (only in the browser).

Your view can do whatever it needs to do. You are not limited by what you see in examples.

Sorry what i mean was 2 different views

(I knew what you meant there.)

Yes, there are typically 2 different views involved.

The first view creates the page being rendered.

The second view accepts the AJAX POST request from the JavaScript and returns the data for the updated select box.

Ok, thanks for the clarification !

Can u point out where im doing it wrongly? I have the following view:

def device_port(request):
    devices = Device.objects.all()    
            selected =""
    if request.method == "POST":
        devices = Device.objects.get(pk=selected)        
        tablename = 'dev_interface_'+selected
        cursor.execute(f"SELECT interface FROM {tablename} WHERE id >=2")
        righttable = cursor.fetchall()                                  
        return redirect('/device/port/selected',{'devices':devices, 'selected': selected, 'righttable':righttable} )    
        return render(request, 'interface/device_port.html',{'devices':devices, 'selected': selected} )
def device_port_selected(request)
    return render(request, 'interface/device_port_selected.html',{'devices':devices} )

I cannot seem to pass my key selected to the next page. I can pass it if i return render('interface/device_port_selected',{'devices':devices, 'selected': selected, 'righttable':righttable} ) but it will also mean the url does not change at all.

Review the docs on the redirect function. You’re not using it properly.

By the way, your code for handling the input from the browser is vulnerable to a SQL injection attack. The first malicious user to use this is capable of deleting all data.

Issue solved for the passing of keys between the 2 page.

For the SQL issue, it there some other method to call the table? This is because the table is created with raw SQL and i was taught that tables that are created with raw SQL cannot be like those models where u from .models import ___. So to call them i use raw SQL to call them