Python Forum
Populate Dropdown list from Database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populate Dropdown list from Database
#1
I'm just starting out with Python, which I think is amazing, but I've not really grasped it yet. So I'm probably doing it completely wrong but here goes. I'm starting with just populating the dropdown.

I have an HTML form add_investor.html. This has the form that posts to the database. At the top of this page, I want to add a dropdown to be able to select the previously added investors.

The only way I can get this work is if I create a new URL and separate function view outside of the class addInvestor and render the view in a new HTML page get_investor.

And I understand that the view is linked to the URL, so what I tried to do is add the function to the class addInvestor and just use the existing URL to render the button within the existing page, but when I click the button I just get an empty dropdown.
addInvestor class - with the getInvestor function:

class addInvestorView(View):
    template_name = 'pages/add_investor.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)

    def post(self, request, *args, **kwargs):
        form = addInvestorForm(request.POST)
        if form.is_valid():
            form.save()
            data = {"success": "successfully added"}
        else:
            data = {"error": form.errors}
        return JsonResponse(data, safe=False)
    
    def getInvestor(self, request, *args, **kwargs):
        results=Investor.objects.all()
        return render(request, self.template_name,{"Investor":results})

        class Meta:
            db_table="apps_investor"
    
Standalone function which works. (Once I've updated the urls)
def getInvestor(request):
    results=Investor.objects.all()
    return render(request,"pages/get_investor.html",{"Investor":results})
   
    class Meta:
        db_table="apps_investor"
It doesn't look right the function within the class, and im not sure how the button calls the getInvestor function?

HTML button:
                    <div class="col-lg-2">
                        <div class="dropdown">
                            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">Update Existing Investor <i class="mdi mdi-chevron-down"></i></button>
                            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                {% for results in Investor %}
                                <option> {{results.investor_name}} </option>
                                {% endfor %}
                            </div>
                        </div>
                    </div><!-- end col -->
Any clues/pointers would be amazing.

Thanks.
Reply
#2
Remember that HTTP is request/response - the client has to request the data it wants and do something with it. The button click obviously happens on the client, so you could write some JavaScript to make a request on the click and put in in the page when you receive the response. Look up AJAX for more on this.
Reply
#3
(Nov-02-2021, 04:42 PM)ndc85430 Wrote: Remember that HTTP is request/response - the client has to request the data it wants and do something with it. The button click obviously happens on the client, so you could write some JavaScript to make a request on the click and put in in the page when you receive the response. Look up AJAX for more on this.
Right, I understand I think. This would explain why it works when going direct to get_investor because it sends a GET request to the getInvestror function?

Is this standard then for rendering multiple views within a single page? You need to use a trigger on Java script to send the request?

But how do i tell JavaScript to send the request to the getInvestor function within the class, rather than the GET
Reply
#4
Flask is very good for web based development, easier than Django which is the other commonly used platform.

Here is an article addressing filling a dropdown with info from a database using Flask.
Reply
#5
(Nov-02-2021, 04:57 PM)TommyAutomagically Wrote: Right, I understand I think. This would explain why it works when going direct to get_investor because it sends a GET request to the getInvestror function?

Essential yes, you can think of it like that. The GET request is sent to the server which forwards it to the right handling function (you haven't shown how that's configured, but from memory Django uses a file somewhere to contain that mapping).

Quote:Is this standard then for rendering multiple views within a single page? You need to use a trigger on Java script to send the request?

If you're really talking about updating the page on some event (like the button click you described above), then yes, AJAX is a standard way to do it.

Quote:But how do i tell JavaScript to send the request to the getInvestor function within the class, rather than the GET

Perhaps you're misunderstanding, or just have the terminology wrong, I'm not sure. The JavaScript code would have to send a GET request to get the data, to whatever path is meant to serve it (whether that path is mapped to that function or another one).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting barplots from the dropdown menu v_mn 0 721 Sep-14-2022, 10:48 AM
Last Post: v_mn
  how to populate a dataframe thru line iteration ? knob 0 1,004 May-05-2022, 12:48 AM
Last Post: knob
  open the html page from the django dropdown menu? shams 2 3,338 Jul-17-2021, 08:10 AM
Last Post: shams
  Auto-populate Macro variables Spartan314 3 2,675 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  Python3 doesn't populate xlsx file with openpyxl Auldyin75 2 2,546 Feb-16-2021, 12:00 PM
Last Post: Auldyin75
  create new tabs and populate using python reggie4 2 2,274 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Populate a table with variables Market_Python 4 2,593 Jan-11-2021, 09:45 AM
Last Post: Pedroski55
  Store a python list of lists in a database to use for searches later on klllmmm 3 3,081 Jun-20-2019, 07:54 AM
Last Post: buran
  Creating a persistent list/database Trinx 1 2,258 Feb-27-2019, 06:20 PM
Last Post: ichabod801
  how to insert list into database zubair 2 9,950 Feb-05-2019, 12:16 PM
Last Post: rajesh1997

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020