Django - include template tags - django

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>

Related

Change login label in Django auth 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.

Unable to add Forgot Password link in Django Admin suit?

In my admin site, I have installed a Django suit and here I am trying to add forgot password link but can't add it Before installing the Django suit I added forgot password link and it was working perfectly but after installing suit it's not working. do you have any idea ??
login.html:
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block content %}
<form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %}
<div class="form-row">
{{ form.username.errors }}
{{ form.username.label_tag }} {{ form.username }}
</div>
<div class="form-row">
{{ form.password.errors }}
{{ form.password.label_tag }} {{ form.password }}
<input type="hidden" name="next" value="{{ next }}">
</div>
{% url 'admin_password_reset' as password_reset_url %}
{% if password_reset_url %}
<div class="password-reset-link">
{% trans 'Forgotten your password or username?' %}
</div>
{% endif %}
Forgot password?
<div class="submit-row">
<label> </label><input type="submit" value="{% trans 'Log in' %}">
</div>
</form>
</div>
{% endblock %}

csrf_token missing and how to get details for specific id entered by user

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 %}

How can I modify the class for a button in a django widget form?

I'm trying to set the class for a form widget. I have:
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.input.label_tag }} {{ form.input.help_text }}</p>
<p>
{{ form.input.errors }}
{{ form.input }}
</p>
<p>{{ form.non_field_errors }}</p>
<p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p>
<p>
{{ form.itinerary.errors }}
{{ form.itinerary }}
</p>
<p><input class="button-primary" type="submit" value="Upload" /></p>
</form>
and it looks like
I attempted to use django tweak tools when I tried googling to find a solution for my problem. I modified the code to be like this too see how it would change:
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% load widget_tweaks %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.input.label_tag }} {{ form.input.help_text }}</p>
<p>
{{ form.input.errors }}
{{ form.input }}
{% render_field form.input class="button" %}
</p>
<p>{{ form.non_field_errors }}</p>
<p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p>
<p>
{{ form.itinerary.errors }}
{{ form.itinerary }}
</p>
<p><input class="button-primary" type="submit" value="Upload" /></p>
</form>
and that looks like
It applies the class to the whole widget, and it turns the whole widget into a button.
So how can I apply the class to just the button in the widget?
So there really isn't an easy way to add a class to a form element.
The way I was able to solve this was to create a custom template tag.
If you interested in learning more about this you can visit the following link: https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/
What I did was create a directory under the app directory called templatetags.
Make sure you have an __init__.py in this directory, and then create another file. I named mine template_helper.py
# template_helper.py
from django import template
from django.utils.datastructures import SortedDict
register = template.Library()
#register.filter(name='addcss')
def addcss(field, css):
return field.as_widget(attrs={"class":css})
In the actual template you would just have to first load the template_helper via: {% load template_helper %} then add the {{ form_element|addcss:"button" }}
You'll want to somehow check with the template tags {% if ... %} to
make sure you only apply the class to the button if you only want to
apply the class to the button.
{% load template_helper %}
<form action="{% url "list" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.input.label_tag }} {{ form.input.help_text }}</p>
<p>
{{ form.input.errors }}
{% for field in form.input %}
{{ field|addcss:"button" }}
{% endfor %}
</p>
<p>{{ form.non_field_errors }}</p>
<p>{{ form.itinerary.label_tag }} {{ form.itinerary.help_text }}</p>
<p>
{{ form.itinerary.errors }}
{{ form.itinerary }}
</p>
<p><input class="button-primary" type="submit" value="Upload" /></p>
</form>
Good Luck!

How to manually render a part of a django form

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>.