How to pass value from html to django view?
I have an option list in html.
<select>
<option value="Please select" disabled selected>- Please Select -</option>
<option value="Iphone">Iphone</option>
<option value="Samsung">Samsung</option>
</select>
When the user select one of the option, the particular table with data will appear.
In views.py:
dev_model = Device.objects.filter(name='name').all()
I want to get the data from html, then filter the option with my database in field 'name'
.
If they are same, the data will be retrieved and display in html.
django
add a comment |
I have an option list in html.
<select>
<option value="Please select" disabled selected>- Please Select -</option>
<option value="Iphone">Iphone</option>
<option value="Samsung">Samsung</option>
</select>
When the user select one of the option, the particular table with data will appear.
In views.py:
dev_model = Device.objects.filter(name='name').all()
I want to get the data from html, then filter the option with my database in field 'name'
.
If they are same, the data will be retrieved and display in html.
django
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14
add a comment |
I have an option list in html.
<select>
<option value="Please select" disabled selected>- Please Select -</option>
<option value="Iphone">Iphone</option>
<option value="Samsung">Samsung</option>
</select>
When the user select one of the option, the particular table with data will appear.
In views.py:
dev_model = Device.objects.filter(name='name').all()
I want to get the data from html, then filter the option with my database in field 'name'
.
If they are same, the data will be retrieved and display in html.
django
I have an option list in html.
<select>
<option value="Please select" disabled selected>- Please Select -</option>
<option value="Iphone">Iphone</option>
<option value="Samsung">Samsung</option>
</select>
When the user select one of the option, the particular table with data will appear.
In views.py:
dev_model = Device.objects.filter(name='name').all()
I want to get the data from html, then filter the option with my database in field 'name'
.
If they are same, the data will be retrieved and display in html.
django
django
edited Nov 16 '18 at 5:15
YvKl
asked Nov 16 '18 at 4:42
YvKlYvKl
1066
1066
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14
add a comment |
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14
add a comment |
2 Answers
2
active
oldest
votes
You may want to use a Django Form to accomplish this.
In a file called forms.py
(in the same directory as your models.py
) you will have something like:
from django import forms
class MyForm(forms.Form):
NAME_CHOICES = (('Iphone', 'Iphone'),
'Samsung', 'Samsung'))
name = forms.ChoiceField(choices=NAME_CHOICES)
Then, in your view, pass the form through context and render it in your django template. I would suggest ModelForm
instead of Form
a lot of the time, but I don't have any specifics about your application/use case.
When the form is POST
ed back to your view, you should be able to get the data by using
form=MyForm(request.POST)
if form.is_valid():
name = form['name'].value()
The link to the documentation should be a good point to get you started
To display the data as soon as the user selects the correct field, you can do this with one of two approaches:
Use
ajax
if you want the filtering to happen on the server sideUse DataTables to pass all the data to the browser and have the user filter their display at their convenience.
You question is too broad to provide a specific answer while lacking the necessary details behind your attempt I have provided a starting point for your forms and links to the necessary documentation. I hope this helps
add a comment |
You can keep mapping of device to a particular value like this:-
DEVICE_TO_MAPPING = {
0:'Samsung',
1:'Iphone',
}
And then use this mapping to send in context as variable like "devicemapping" to render in html in template, you can do it like.
<form name="device-form" method='POST' action='.'>
<select>
<option value="{{device_value}}" disabled selected>- Please Select -</option>
{% for device_value, device_name in devicemapping.items %}
<option name='{{device_value' value="{{device_value}}">{{ device_name }}</option>
{% endfor %}
</select>
</form>
In your views.py you can filter the data using Mapping.
device_value = request.POST.get('device_value')
device_name = DEVICE_TO_MAPPING.get(device_value)
dev_model = Device.objects.filter(name=device_name)
i get this error:Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.
– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331565%2fhow-to-pass-value-from-html-to-django-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may want to use a Django Form to accomplish this.
In a file called forms.py
(in the same directory as your models.py
) you will have something like:
from django import forms
class MyForm(forms.Form):
NAME_CHOICES = (('Iphone', 'Iphone'),
'Samsung', 'Samsung'))
name = forms.ChoiceField(choices=NAME_CHOICES)
Then, in your view, pass the form through context and render it in your django template. I would suggest ModelForm
instead of Form
a lot of the time, but I don't have any specifics about your application/use case.
When the form is POST
ed back to your view, you should be able to get the data by using
form=MyForm(request.POST)
if form.is_valid():
name = form['name'].value()
The link to the documentation should be a good point to get you started
To display the data as soon as the user selects the correct field, you can do this with one of two approaches:
Use
ajax
if you want the filtering to happen on the server sideUse DataTables to pass all the data to the browser and have the user filter their display at their convenience.
You question is too broad to provide a specific answer while lacking the necessary details behind your attempt I have provided a starting point for your forms and links to the necessary documentation. I hope this helps
add a comment |
You may want to use a Django Form to accomplish this.
In a file called forms.py
(in the same directory as your models.py
) you will have something like:
from django import forms
class MyForm(forms.Form):
NAME_CHOICES = (('Iphone', 'Iphone'),
'Samsung', 'Samsung'))
name = forms.ChoiceField(choices=NAME_CHOICES)
Then, in your view, pass the form through context and render it in your django template. I would suggest ModelForm
instead of Form
a lot of the time, but I don't have any specifics about your application/use case.
When the form is POST
ed back to your view, you should be able to get the data by using
form=MyForm(request.POST)
if form.is_valid():
name = form['name'].value()
The link to the documentation should be a good point to get you started
To display the data as soon as the user selects the correct field, you can do this with one of two approaches:
Use
ajax
if you want the filtering to happen on the server sideUse DataTables to pass all the data to the browser and have the user filter their display at their convenience.
You question is too broad to provide a specific answer while lacking the necessary details behind your attempt I have provided a starting point for your forms and links to the necessary documentation. I hope this helps
add a comment |
You may want to use a Django Form to accomplish this.
In a file called forms.py
(in the same directory as your models.py
) you will have something like:
from django import forms
class MyForm(forms.Form):
NAME_CHOICES = (('Iphone', 'Iphone'),
'Samsung', 'Samsung'))
name = forms.ChoiceField(choices=NAME_CHOICES)
Then, in your view, pass the form through context and render it in your django template. I would suggest ModelForm
instead of Form
a lot of the time, but I don't have any specifics about your application/use case.
When the form is POST
ed back to your view, you should be able to get the data by using
form=MyForm(request.POST)
if form.is_valid():
name = form['name'].value()
The link to the documentation should be a good point to get you started
To display the data as soon as the user selects the correct field, you can do this with one of two approaches:
Use
ajax
if you want the filtering to happen on the server sideUse DataTables to pass all the data to the browser and have the user filter their display at their convenience.
You question is too broad to provide a specific answer while lacking the necessary details behind your attempt I have provided a starting point for your forms and links to the necessary documentation. I hope this helps
You may want to use a Django Form to accomplish this.
In a file called forms.py
(in the same directory as your models.py
) you will have something like:
from django import forms
class MyForm(forms.Form):
NAME_CHOICES = (('Iphone', 'Iphone'),
'Samsung', 'Samsung'))
name = forms.ChoiceField(choices=NAME_CHOICES)
Then, in your view, pass the form through context and render it in your django template. I would suggest ModelForm
instead of Form
a lot of the time, but I don't have any specifics about your application/use case.
When the form is POST
ed back to your view, you should be able to get the data by using
form=MyForm(request.POST)
if form.is_valid():
name = form['name'].value()
The link to the documentation should be a good point to get you started
To display the data as soon as the user selects the correct field, you can do this with one of two approaches:
Use
ajax
if you want the filtering to happen on the server sideUse DataTables to pass all the data to the browser and have the user filter their display at their convenience.
You question is too broad to provide a specific answer while lacking the necessary details behind your attempt I have provided a starting point for your forms and links to the necessary documentation. I hope this helps
edited Nov 16 '18 at 5:57
answered Nov 16 '18 at 5:49
robotHamsterrobotHamster
345217
345217
add a comment |
add a comment |
You can keep mapping of device to a particular value like this:-
DEVICE_TO_MAPPING = {
0:'Samsung',
1:'Iphone',
}
And then use this mapping to send in context as variable like "devicemapping" to render in html in template, you can do it like.
<form name="device-form" method='POST' action='.'>
<select>
<option value="{{device_value}}" disabled selected>- Please Select -</option>
{% for device_value, device_name in devicemapping.items %}
<option name='{{device_value' value="{{device_value}}">{{ device_name }}</option>
{% endfor %}
</select>
</form>
In your views.py you can filter the data using Mapping.
device_value = request.POST.get('device_value')
device_name = DEVICE_TO_MAPPING.get(device_value)
dev_model = Device.objects.filter(name=device_name)
i get this error:Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.
– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
add a comment |
You can keep mapping of device to a particular value like this:-
DEVICE_TO_MAPPING = {
0:'Samsung',
1:'Iphone',
}
And then use this mapping to send in context as variable like "devicemapping" to render in html in template, you can do it like.
<form name="device-form" method='POST' action='.'>
<select>
<option value="{{device_value}}" disabled selected>- Please Select -</option>
{% for device_value, device_name in devicemapping.items %}
<option name='{{device_value' value="{{device_value}}">{{ device_name }}</option>
{% endfor %}
</select>
</form>
In your views.py you can filter the data using Mapping.
device_value = request.POST.get('device_value')
device_name = DEVICE_TO_MAPPING.get(device_value)
dev_model = Device.objects.filter(name=device_name)
i get this error:Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.
– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
add a comment |
You can keep mapping of device to a particular value like this:-
DEVICE_TO_MAPPING = {
0:'Samsung',
1:'Iphone',
}
And then use this mapping to send in context as variable like "devicemapping" to render in html in template, you can do it like.
<form name="device-form" method='POST' action='.'>
<select>
<option value="{{device_value}}" disabled selected>- Please Select -</option>
{% for device_value, device_name in devicemapping.items %}
<option name='{{device_value' value="{{device_value}}">{{ device_name }}</option>
{% endfor %}
</select>
</form>
In your views.py you can filter the data using Mapping.
device_value = request.POST.get('device_value')
device_name = DEVICE_TO_MAPPING.get(device_value)
dev_model = Device.objects.filter(name=device_name)
You can keep mapping of device to a particular value like this:-
DEVICE_TO_MAPPING = {
0:'Samsung',
1:'Iphone',
}
And then use this mapping to send in context as variable like "devicemapping" to render in html in template, you can do it like.
<form name="device-form" method='POST' action='.'>
<select>
<option value="{{device_value}}" disabled selected>- Please Select -</option>
{% for device_value, device_name in devicemapping.items %}
<option name='{{device_value' value="{{device_value}}">{{ device_name }}</option>
{% endfor %}
</select>
</form>
In your views.py you can filter the data using Mapping.
device_value = request.POST.get('device_value')
device_name = DEVICE_TO_MAPPING.get(device_value)
dev_model = Device.objects.filter(name=device_name)
edited Nov 16 '18 at 8:03
answered Nov 16 '18 at 6:11
Ritesh BishtRitesh Bisht
515
515
i get this error:Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.
– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
add a comment |
i get this error:Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.
– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
i get this error:
Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.– YvKl
Nov 16 '18 at 7:29
i get this error:
Could not parse the remainder: '()' from 'devicemapping.items()'
. I tried to remove the (), but still can't get the data.– YvKl
Nov 16 '18 at 7:29
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
And also my database is huge, if using direct mapping, it would be very complicated.
– YvKl
Nov 16 '18 at 7:38
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
can you please share your html and python code, and if you do not want to keep mapping you can fetch the id and name from database and send in your context as dictionary
– Ritesh Bisht
Nov 16 '18 at 7:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331565%2fhow-to-pass-value-from-html-to-django-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
<select class="chosen" style="width:350px" multiple="true">
– Hardik Gajjar
Nov 16 '18 at 6:14