Nov-02-2021, 12:18 PM
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:
HTML button:
Thanks.
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.