I try to post value of input buttons in Django but I couldn't
This is my template
<form id="ReviewRateForm" method="post" action="/review/post/rate/">
<input type="button" hint="V1" title="V" value="1" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
<input type="button" hint="V1" title="V" value="2" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
<input type="button" hint="V1" title="V" value="1" id="radio{{ forloop.counter }}-1" type="button" name="qid[{{forloop.counter}}]"></input>
</form>
However, when I debug it I couldn't reach the values of that input buttons in my view.
What is the problem or how can I overcome it?
The values can be accessed by the name of the input from request.POST. However, you're dynamically naming the inputs, which is going to make things more complicated when you go to retrieve those values.
Example without taking into consideration the dynamic naming:
quid1 = request.POST.get('quid1')
The problem might be with your browser rather than with django.
If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute.
Update: Oh, you are not using <button> elements, I read too fast. Sorry. Then this answer is not relevant.
Related
I am looking into uploading a file from the html template. I've seen a fair amount of documentation including FileFields, ImageFields etc. However, ideally I do not want to rewrite my code.
Currently, I have a simple form on my template and I would like to have an upload function there, where, an image will be uploaded and stored into my applications media folder and if possible added to a database.
I do know that I've probably taken a long and complex route but if anyone can help it'll be great!
html.py:
<div class="row"> <div class="span1 offset5"> </bR>
<form class="form-horizontal" method="get" action="/add/submit" value="add">
<fieldset> <div class="input">
<div class="inline-inputs">
<label>Ride Name:</label><input type="text" name="name">
<label>Type:</label><input type="text" name="type">
<label>Theme:</label><input type="text" name="theme">
<label>Description:</label><textarea rows="5" name ="description"></textarea>
<label>Author:</label><input type="text" name="author">
<label>Date Released:</label>
<div id="datetimepicker" class="input-append date">
<input type="text" name="date"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<label>Thread:</label><input type="text" name="thread">
<label>Video</label><textarea rows="2" name ="video"></textarea>
<br><br>
<input class="btn btn-primary" type="submit" value="Add" />
</div> </div>
</fieldset>
</form>
</div> </div>
Currently my Views.py just takes the entered data and inserts it into a database. I want to add the ability for a file to be uploaded:
def Ride_Add_Submit(request):
name = request.GET['name']
type = request.GET['type']
theme = request.GET['theme']
description = request.GET['description']
author = request.GET['author']
releasedate=request.GET['date']
video=request.GET['video']
thread=request.GET['thread']
entry = RollerCoaster(name=name, type=type, theme=theme, description=description, releasedate=releasedate, author=author, video=video, thread=thread)
entry.save()
return TemplateResponse(request, 'Ride_Add.html')
I don't understand why you keep talking about the template here, the template has nothing whatsoever to do with anything. The handling of the upload, like all logic, is done in the view.
The file upload overview still has all the information you need. You can ignore the parts about the Django form and checking if it's valid, and simply pass the file object to your upload handling function, which that page also explains.
However you will need to change your template so that the form element uses POST instead of GET (which is almost certainly a good idea anyway), and use enctype="multipart/form-data" as also described on that page.
Finally, I really would advise you to rewrite your code to use ModelForms. Not only would it make your code much simpler, it would also do things like validate the entry to make sure all the required fields are present and are of the right types, and so on - as well as output valid HTML (for instance, you're missing for attributes in your label tags).
I do a lot of re-rendering templates (adding elements, removing elements) using AJAX. Is there an easier way to do this where I could define certain elements ahead of time and pass in a Javascript associative array and it would return the HTML for me? I have heard suggestions for Handlebars, but I heard that it conflicts with Django's templating system.
An example of this is:
<div id="overlay"></div>
<div id="add-action-box" class="lightbox">
<a class="close" href="#">X</a>
<h2>Add Goal</h2>
<form class="lightbox-form" id="add-action-form">
<div class="lightbox-form-element">
<label>Goal</label>
<input type="text" />
</div>
<div class="lightbox-form-element">
<label>Deadline</label>
<input type="text" />
</div>
<div class="lightbox-form-element">
<input type="submit" value="Add Goal" />
</div>
</form>
</div>
I want to be able to define the above HTML elsewhere and allow Jquery to append the above to another element. I don't want to type in the above and delete all of the newlines, add \, put quotes around everything, etc. It makes it harder to read.
It is frustrating to edit HTML when it is on the same line like this:
$("body").append('<div id="add-action-box" class="lightbox"><a class="close" href="#">X</a><h2>Add Goal</h2>')
Have you considering using something similar to jQuery Templates. They are no longer maintained and the project has been picked up by jQuery UI. More details can be found here. I usually find them helpful in situations like yours.
I have a form in a django site
<form method="POST" action="." class="right_custom">{% csrf_token %}
<br>{% trans "Enter the discount coupon code if you have any" %}</br>
<input type="text" name="coupon_code" size="25" maxlength="25" />
<input type="submit" name="submit" value="Caluclate Discount"/>
</form>
I would like to translate the entire site to a lot of languages. I need to translate the button text which is Caluclate Discount. How can I do that? if i use {% trans %} tag, how will the view catch the right post request?
UPDATE
There are many forms on the same page like this and my view uses if postdata['submit']=="Caluclate Discount" to determine which submit request it is.
I was able to get the translation working.
Thanks to the answers by #linux-warrior and #Joachim
Now the form is
<form method="POST" action="." class="right_custom">{% csrf_token %}
<input type="hidden" name="form_name" value="discount_form" />
<br>{% trans "Enter the discount coupon code if you have any" %}</br>
<input type="text" name="coupon_code" size="25" maxlength="25" />
<input type="submit" name="submit" value="{% trans "Caluclate Discount" %}" />
</form>
And i check for if postdata['form_name']=='discount_form' in my view
For buttons, you really don't use the value field for anything else than the button text, so it is straightforward to translate:
<input type="submit" name="submit" value="{% trans "Caluclate Discount" %}"/>
I think that you should use {% trans %} for submit "value". I don't understand why would you need that value inside your view. If you want, you can still give your submit input a custom "name" attribute.
Edit. By the way, your
<br>...</br>
thing inside your form appears to be a bug. You will probably want to make it
<p>...</p>
instead. It is also not recommended to use "submit" name for a type="submit" input (taken from http://api.jquery.com/submit/):
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.
Your view doesn't care about what is the submit button's value, so even if you translate it, your view function will work.
I'm trying to do some pretty basic form posts with Django, but whenever I try to click on the button to submit the information nothing happens. No errors or messages of any kind show up in terminal or in developer in Chrome. There is no JS on this page just straight html:
<form method="post" action="/">
{% csrf_token %}
<input type="text" id="name" name="name"/>
<input type="text" id="password" name="password"/>
<input type="button" value="Sign Up!"/>
</form>
My view for this page is pretty straightforward as well:
def sign_up(request):
return render_to_response('portal/signup.html', context_instance=RequestContext(request))
I'm really baffled as to what is going on, I've been following this to learn authentication. Everything works but I thought adding a "create user" would be a next step. I can't seem to get any form of any kind to work on other pages as well.
Any help would be great, I'm going crazy!
I think that your problem is that you're using
<input type="button" value="Sign Up!"/>
instead of
<input type="submit" value="Sign Up!"/>
the input submit will send all the form data to the server, the input button won't.
You can learn a little bit more about forms here : http://www.w3schools.com/html/html_forms.asp
Django accepts and works with form button in format:
<input type="submit" value="Submit" />
How to make it to work with the following:
<button type="button">Submit</button>
Could you explain what exactly you mean by "Django accepts and works with form button in format"? As far as I know the difference in the behaviour of <input type="submit" .../> and <button type="button">... has nothing to do with Django. Django does not care how a form is submitted from the browser.
This previously posted question might be of interest.
Add the following to the button to submit the form: ("myform" is the id of your form)
onClick="javascript.document.myform.submit();"