Passing Variable From Template.html To Views.py By URL (using A File Input Form)
Maybe this is a very silly question but I'm new using django.
I have a certain django template with a set of properties that can be chosen. On an uploaded file I have to perform a different treatment depending on the chosen property, so I want to pass the chosen property value from the template to the view. Properies are hard coded in view.py and retrieved in the template by getJSON:
<script type="text/javascript">
$(function() {
$.getJSON('available_properties', function(data) {
var options = [];
for(var i in data.properties)
{
var prop = data.properties[i];
options.push('<option value="'+prop+'">'+prop+'</option>');
}
$("#properties > select").html(options.join(''));
});
});
</script>
Then an uploading file form trigger the action:
<form onsubmit="return checkfile();" prop="return get_prop();" action="calculate/<property_value_goes_here>" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<input type="file" id="uploadfile" name="uploadfile" size="30">
<input type="submit" id="calculate" value="Calculate!">
</form>
How can I pass the property value directly in the URL path that must be called on the submit form?
Answer
Actually you have two ways to solve that problem. One is parsing the url, the other one is passing the properities as request parameters.
The first one can be implemented like this:
urlpatterns = patterns('',
(r'^/(?P<property>\*)/$', 'appname.viewname'),
)
in your url, and:
def your_view(request,property):
#your view stuff here
in views.py of your app. This is documented in Part 3 of the django tutorial, as you can find it here: http://docs.djangoproject.com/en/1.2/intro/tutorial03/
But for your problem the second solution is probably the better on. You can create a standard url-querystring (as your post suggests you know how to use js to do that) like this:
/upload/?property=value&someotherproperty=othervalue
and get the passed values like this:
def your_view(request):
property = request.POST.get('property',None)
someotherproperty = request.POST.get('someotherproperty',None)
where the get works as:
request.POST.get(property_name,fallback_value)
To put the querystring into request.POST, you have to configure your urls.py like this:
urlpatters = patterns('',
(r'^upload/$','appname.viewname'),
)
That way everything after /upload/ will be passed to request.POST
Related Questions
- → Django, code inside <script> tag doesn't work in a template
- → Uncaught ReferenceError: Parent is not defined
- → React - Django webpack config with dynamic 'output'
- → Put a Rendered Django Template in Json along with some other items
- → Implement shopify templates in django
- → Python Shopify API output formatted datetime string in django template
- → How to avoid being crawled/penalized by Google
- → Django: Identify the urls that provide duplicate content and set a canonical link
- → Shopify app: adding a new shipping address via webhook
- → Jquery Modal Confirmation on Django form submit for deletion of object
- → changing the size of an image with css
- → shopify_auth multi store session handling
- → How to use Shopify Python API RecurringApplicationCharge