Change login label in Django auth form - django

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.

Related

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

Issue assigning form fields in the HTML template django

I have a model form and I wanted to know if it is possible to take a model form and assign the name of the specific field when it is running through a for loop within the template html file.
Here is what i have in the current html template:
{% extends "base.html" %}
{% block content %}
<h1>Add members to {{record.name}}</h1>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{% for trans in transactions %}
{% if trans.record.id == record.id %}
{{ trans.user.username }}
{{ form.as_p }}
{% endif %}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
here is the current form model:
class IndividualSplitTransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ['amount', 'description']
currently the format is looking like this:
omar
amount
description
hani
amount
description
assad
amount
description
tax
tip
so when i process it and i want to grab the amount assigned to each of the specific users and update a record. I want it to look something like this for easier processing in the view.
omar
omaramount
omardescription
hani
haniamount
hanidescription
assad
assadamount
assaddescription
tax
tip
so that when i am processing I can properly assign the corrent amount and description for each user when i am updating existing records with the amount.
is it possible to do that within the template itself or would i have to do it somewhere else.
UPDATED
So i tried to change my code and update it to get what i am trying to get but this is what i am getting...
Here is the html
{% extends "base.html" %}
{% block content %}
<h1>Add members to {{record.name}}</h1>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{% for trans in transactions %}
{% if trans.record.id == record.id %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ trans.user.username }}{{ field.label }}{{ field|safe }}
</div>
{% endfor %}
{% endif %}
{% endfor %}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
and here is what I am getting.
request returs
So if you look at the image, it is still just returning the last amount and description of the three that i entered... should i just create the whole form manually in the html file....
If you wanted the username + amount or username + description, then you to change away from form.as_p, because you will get the html <p><p/> tag for each field with label preceding it.
You want to dynamically add username then do by designing your own html content of the form manually.
There is a label_suffix, but prefix is not available. Even with suffix you should be doing it at the time of creating the form object.
Update:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ trans.user.username }} {{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}

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

javascript alert - after submit in django

I have an html form in my website it's using django, and I want show massage "javascript alert()" after submit successfully
this is the form:
<form action="{% url guestbook-post %}" method="POST">{% csrf_token %}
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
{% if field.errors %}{{ field.errors }}{% endif %}
{{ field.label_tag }} {{ field }}
</p>
{% endif %}
{% endfor %}
<p class="submit">
<input type="submit" name="submit" class="submit-post" value="Post" />
<input type="submit" name="submit" class="submit-preview" value="Preview" />
</p>
</form>
Use jQuery with a submit handler:
$('#myForm').submit( function () { alert('Form Submitted'); });
Not sure how Django handles posts, so you may need to do some further routing within the jQuery call...
You could add a javascript function to the submit handler of the form and have an alert in this function. Something like:
<form action="{% url guestbook-post %}" method="POST" onsubmit="return validate()">
<script>
function validate()
{
// do some validation here to decide whether the post is going to be successful or not.
if (valid)
{
alert("Going to post your data now!");
return true;
}
else
{
return false; // form won't post
}
}
</script>

Django: Redirect after posting a comment

I am trying to redirect the user back to the page where the comment was posted. I found this post on Django's site but I am doing something wrong because it won't redirect back.
Where should the input be placed to have it properly redirected?
{% load comments i18n %}
<form action="{% comment_form_target %}" method="post">{% csrf_token %}
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
{% for field in form %}
{% if field.is_hidden %}
{{ field }}
{% else %}
{% if field.errors %}{{ field.errors }}{% endif %}
<input type="hidden" name="next" value="{% url proposal proposal.id %}" />
<p
{% if field.errors %} class="error"{% endif %}
{% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}
{% ifequal field.name "name" %} style="display:none;"{% endifequal %}
{% ifequal field.name "email" %} style="display:none;"{% endifequal %}
{% ifequal field.name "url" %} style="display:none;"{% endifequal %}
{% ifequal field.name "title" %} style="display:none;"{% endifequal %}>
<!-- {{ field.label_tag }} -->{{ field }}
</p>
{% endif %}
{% endfor %}
<p class="submit">
<!-- <button><input type="submit" name="post" value="{% trans "Send" %}" /></button> -->
<button type="submit">Send</button>
<!-- <input type="submit" name="preview" class="submit-preview" value="{% trans "Preview" %}" /> -->
</p>
</form>
Maybe you don't need to check for next variable in your template. You could try changing:
{% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
to just:
<input type="hidden" name="next" value="/added/comment/page/" />
In case you use views.py, redirecting from there seems more obvious, at least for me, as it helps keep the concern away from the template:
from django.http import HttpResponseRedirect
HttpResponseRedirect("/path/to/redirect")
The problem with axel22's answer is that it requires a change to each template that requires the comment form - if you have multiple object types that can be commented on, this is not DRY.
Unfortunately, I'm also still looking for an answer that works.
if you are using {% render_comment_form for object %} tag in your template, just add something like {% url object's_named_view object.id as next %} or wrap it with {% with object.get_absolute_url as next %} ... {% endwith %} construction.
See my solution here: Django: Redirect to current article after comment post
It basically uses a view that's triggered by the comment post url which redirects back to the original referrer page.