Python Forum

Full Version: Populate Dropdown list from Database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.
(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
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.
(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).