I actually render a django form with the following code into template.html :
<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Envoyer" />
</form>
One of the field is rendered with a checkbox widget, with a lot of values, so I would like to put them in a table.
Is there a way to do this without manually render the whole form ?
You can simply use following lines (with their corresponding errors) :
<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
<li>
{{ form.nameOfField1 }}
{{ form.nameOfField1.errors }}
</li>
<li>
{{ form.nameOfField2 }}
{{ form.nameOfField2.errors }}
</li>
<input type="submit" value="Envoyer" />
</form>
Here is the doc for 1.6 : https://docs.djangoproject.com/en/1.6/topics/forms/#customizing-the-form-template
Also, as you have many fields, you can use some code like this :
<form action="{% url "demande_create_form" %}" method="post">{% csrf_token %}
{% for field in form %}
<li>
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</li>
{% endfor %}
<input type="submit" value="Envoyer" />
</form>
Of course, you can use anything else than <li>.
Related
I have to submit a form via POST method.
I added to my html form the {% csrf_token %} tag but from the DOM I'm getting this:
<input type="hidden" name="csrfmiddlewaretoken" value="">
But if I type {{ csrf_token }} in the template I can see the value.
Why can't I set my token in the form?
This is my form:
<form action="{% url 'aj_upload_file' %}" method="POST">
{% csrf_token %}
<a id="upload-file-btn" href="javascript:;" class="btn btn-success mr-2">
<i class="fa fa-upload" aria-hidden="true"></i>
</a>
<input class="d-none" multiple type="file" id="upload-file">
</form>
Thank You.
This code is saving my form
<form method="POST" role="form" enctype="multipart/form-data" id="form-container">
{{ my_form|crispy }}
<button type="submit">Submit form</button>
</form>
But when I use as_crispy_field, It is not submitting at all.
<form method="POST" role="form" enctype="multipart/form-data" id="form-container">
{{ my_form.lastName|as_crispy_field }}
{{ my_form.firstName|as_crispy_field }}
{{ my_form.middleName|as_crispy_field }}
{{ my_form.birthData|as_crispy_field }}
{{ my_form.nation|as_crispy_field }}
{{ my_form.birthPlace|as_crispy_field }}
<button type="submit">Submit form</button>
</form>
<form method="POST" role="form" enctype="multipart/form-data" id="form-container">
{% csrf_token %}
{{ my_form|crispy }}
<button type="submit">Submit form</button>
</form>
I'm trying to change the login username label but without any success. Here is the website:
What I'd like to do is print "Usuário and e-mail" instead of just "Usuário" label:
I didn't customize the login form, in this way, I'm using all standard auth process.
#template
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Login"></p>
</form>
If you guys have any suggestion modifying the input field through CSS style, it's welcome as well!
Thanks!
EDIT:
{% for field in form %}
<div class="fieldWrapper">
{% if field.label_tag == 'Usuário' %}
{% field.label_tag = 'Usuário ou e-mail:' %}
{% elif %}
{{ field.label_tag }}
{% endif %}
{{ field.label_tag }}{{ field }}
</div>
{% endfor %}
Value the label in the attribute form.
I am trying to display the result for the specific id entered by the user. I 'm not sure about the view.py file also. What changes should I need to make to get the desired result?
view.py file
def show(request):
if request.method == 'POST':
Num = allData.objects.only('emp_no')
data = request.POST.get('emp_no')
if data.is_valid():
for n in Num:
if n == data:
empid = data
emp = {'emp_no':data}
return render(request,'system/show.html',{'emp_no':data})
return(data.errors)
return HttpResponse("<h2>OOPS!! NO RECORD FOUND</h2>")
show.html
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
</div>
{% endblock %}
Welcome to SO!
From what you have there it looks like it would be worthwhile taking a look at the Django forms documentation.
The main thing that jumps out to me is that you are trying to reuse the same template both for the form and for the data display after the form is submitted. It might be easier to separate them.
If you do want to keep one template, then you don't want to show the form if there is data and vice versa - if there is no data, then you want to show the form. It would look something like this:
{% if emp_no %}
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
{% else %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% endif %}
The title of your question also mentions csrf_token problems. You didn't give any details, but my guess is things are getting confused because you are loading the form even when you don't need it.
I will point out a few things before trying to post a solution.
Your code is not that readable because of the way you name variables. Try naming your variables in a way that tries to reflect what your code is doing.
I will suggest that you finish django tutorial if you haven't done that already. It will help you grab the core concepts of Django.
From what I understood you are trying to get an employee with a given employee number.
This is how I would do it.
In the views.py I would user render instead of HttpResponse.
def show(request):
context = {}
if request.method == 'POST':
# extract the emp no from the request
emp_no = request.POST.get('emp_no')
# get or create the employee with this emp_no
current_employee = Employee.objects.get_or_create(emp_no)
context['current_employee'] = current_employee
return render(request, 'show.html', context)
In the templates, you don't need a forloop. You are retrieving only one employee.
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
<div>
{{ current_employee.GENDER_CHOICE}}
{{ current_employee.first_name }}
{{ current_employee.last_name }}
{{ current_employee.birth_day }}
{{ current_employee.hire_date }}
{{ current_employee.dept_no }}
{{ current_employee.dept_name }}
{{ current_employee.salary }}
{{ current_employee.from_date }}
{{ current_employee.to_date }}
{{ current_employee.titles }}
</div>
</div>
{% endblock %}
Currently I'm using a template which I include in my main template to display a form and submit it back. Problem is that none of the submit buttons work inside that template but only if they are placed inside the main template.
My code is:
{% csrf_token %}
{{ exampleForm.management_form }}
{% for form in exampleForm %}
<form onsubmit="return false;" method="GET" class="exaSubmit" enctype="multipart/form-data">
<div id="example1" type="hidden">
{{ exampleForm.management_form }}
( {{ form.letterOfWord }} + {{ form.keyToUse }} ) MOD 26 =
{{ form.letterToFill }} <button name="action" class="validateButton" value="validate"> Validate </button> <br>
</div>
</form>
{% endfor %}
The validate button does not do anything. It only works when I call it in the main template and not from inside the include template. Any tips?
This might help
{% csrf_token %}
{{ exampleForm.management_form }}
<form onsubmit="return false;" method="GET" class="exaSubmit" enctype="multipart/form-data">
{% for form in exampleForm %}
<div id="example1" type="hidden">
{{ exampleForm.management_form }}
( {{ form.letterOfWord }} + {{ form.keyToUse }} ) MOD 26 =
{{ form.letterToFill }} <br>
</div>
{% endfor %}
<button name="action" class="validateButton" value="validate"> Validate </button>
</form>