0

I want to have multiple dropdowns, each showing the available objects from one specific model.

So Dropdown 1:

Apples
Oranges
Pears

and, Dropdown 2:

Apples
Oranges
Pears

etc.

The bonus question is to have these dropdowns linked/be dependent so that as the user selects items, these chosen items are removed from the remaining dropdowns.

Is this possible?

1

2 Answers 2

1

It is possible. Import your Model to the view-File. Example:

def editUser(request):
    users = "YourModel".objects.all()

    if request.method="POST":
       selected_Item = request.POST['user.id']
       userID = Employee.objects.get(id=selected_item)
       userID.delete()


    context = {
             'user' : users
    }

    return render(request, "your_template_name", context)

So you select your Item by your ID or name. In your Templates you can say "user.your_stuff". So if your Model has things like name you can write user.name. Then delete the stuff.

Context hier is like a Dictonary. You can work with it in your Template.

<form method="POST" > {%csrf_token%}
<select name="user.id">
{% for entry in user %}
    <option>  {{ entry.id }} </option>
{% endfor %}
</select>

<input type = "submit" value="Submit">
</form>

So now you have a DropDown Menu that lists all Entrys from user. You can edit your return in your View so just call the same page and you just "refreshed" the Site and the value you want to delete is away.

Im sorry for my bad english or for my bad explanation. Im still improving my English Skills and im new too StackOverflow and Django too :P If you have any Questions left, im here to help! :)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a for loop within a html list.

index.html

<ul >
{% for element in model %} 
<li class="product"> {{ element }} </li>
{% endfor %}
</ul>

views.py

def index_function(request):
    model = product_model_name.objects.all()
    context = {
        'model': model,
    }
    return render(request, 'index.html', context)

Now you only have to style the ul, there are tons of tutorials with different styles!

Bonus question You can handle this problems using JavaScript. Using addEventListener, this and element.style.display = "none" you can hide a linked div changing its css properties.

4 Comments

I tried this but how do I get "model" to the html, do I pass it from the view like a form? That said, what I actually need is a Django form with multiple dropdowns that contain the same values, and these values are objects from one of my Django Models.
I edited the answer. The code is about a query in views.py then you display it in the html file.
You may have implied it or perhaps I am missing something but this does not create dropdowns
The answer wont create a dropdowns itslef, it just will create automatically the fields inside a list. Now you have to implement the CSS code for the dropdown, Here you have a guide. [ w3schools.com/css/css_dropdowns.asp ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.