Reputation: 103
... I'm new in django and I have this problem: When I want to show the data selected on my dropdown, it doesn't work ... this is the code
index.html
<form method="post" action="/getdata/">
<select name="Lista">
<option selected="selected" disabled>Objects on page:</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
<input type="submit" value="Select">
</form>
views.py
def index(request):
if request.method == 'POST':
Lista = request.POST.get('Lista')
print Lista
context = {
Lista: 'Lista'
}
return render(request, 'showdata.html', context)
else:
template = loader.get_template('index.html')
return HttpResponse(template.render())
getdata.html
<form method="post" action="/getdata/">
<table>
<tr>
<td>Numero:</td>
<td><strong>{{ Lista }}</strong></td>
</table>
</form>
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^', views.home, name="home"),
#define the url getdata that we have written inside form
url(r'^getdata/', views.index),
# defining the view for root URL
url(r'^$', views.index),
]
when I executed the server i just see the following "Numero: " without any number selected ...
Thanks for your help ...
Upvotes: 0
Views: 3972
Reputation: 4138
This is because in the context you are storing the value as the key:
Lista = request.POST.get('Lista')
print Lista
context = {
Lista: 'Lista'
}
If Lista
is 50
, then in your template you would be able to access it as <td><strong>{{ 50 }}</strong></td>
, and the output would be <td><strong>'Lista'</strong></td>
. But this is probably not what you want.
Instead, assign the key in your view like this:
context = {
'Lista': Lista
}
Upvotes: 2