django - How do I change the template being displayed after the user clicks a button? -
i'm quite new django, aplogize if making dumb mistakes.
here code have far:
for views.py:
def bylog(request): if request.post.get('filter'): return render(request, 'index.html', context) filtered_login = invalid.objects.values_list('login').distinct() filtered = [] item in filtered_login: filtered.append(item[0]) results = {'results': results, 'filtered': filtered} return render(request, 'bylog.html', context)
here snippet of bylog.html:
<select id>"dropdown"> {% item in filtered %} <option value={{ item }}">{{ item }}</option> {% endfor %} </select> <input type="submit" value="filter" name="filter" />
my main goal value drop down list, , after user clicks filter
button, value gets passed template.
possible?
thanks help.
the basic goal supose manage post
in django, meaning want send data/variables template view , operation (send template, or store...)
the basic is(using html form, not django form):
- create html form in template - add selects/inputs data want manage , button/input make post - manage post in view
example
template form
<form id="" method="post" action="."> {% csrf_token %} <select id="any_name" name="any_name">"dropdown"> {% item in filtered %} <option value={{ item }}">{{ item }}</option> {% endfor %} </select> <input type="submit" value="filter" name="filter" /> </form>
view.py
def your_view(request): if request.method == 'post': # if clicks filter, receive post method data = request.post['any_name'] # need here data # can call template , send data # can change any_name variable want, changing name , id in select #your view code
i recommend read django forms
because if need bigger form, manage data of model lot of fields, django form
save lot of time
Comments
Post a Comment