Как получить выбранное значение из опции select в моем шаблоне? Django
views.py:
def add(request):
categories = Category.objects.all()
current_user = request.user
owner = current_user
if request.method == "POST":
isActive = True
img = "ASS"
title = request.POST["title"]
description = request.POST["description"]
price = request.POST["price"]
category = request.POST["categories_n"]
f = Listing(title,description, img, price, isActive=True, owner = owner, category)
f.save()
context = {'owner': owner, "categories":categories}
return render(request, "auctions/add.html", context)
Шаблон:
{% block body %}
<div class ="main">
<form action = "{% url 'add' %}" method = "POST">
{% csrf_token %}
<h3>Owner of the item: <strong>{{owner}}</strong></h3>
<ul class = "u_list">
<li class = "l_item"><label for = "t">Title:</label> <input type = "text" name = "title" id = "t" placeholder = "Item's name"></li>
<li class = "l_item"><label for = "d">Description: </label><input type = "text", name = "description" id = "d" placeholder = "Details about the item"></li>
<li class = "l_item"><label for = "p">Price: </label> <input type = "price" name = "price" id = "p" placeholder = "Item's price"></li>
<select name = "categories_n" id = "c">
{% for option in categories %}
<option value = "{{option.id}}" {% if categories_n == option.name %} selected = "selected"{% endif %}>
{{option}}
</option>
{% endfor %}
</select>
<input type = "submit" value = "Submit">
</ul>
</form>
</div>
{% endblock body %}
Как получить опцию из моего шаблона? Что я должен сделать, чтобы получить выбранную опцию? Я не знаю, почему я не использовал Django Forms.
Версия Django: 4.1.4; Python: 3.10.5;