When validating on W3C django is wrapping input fields in <tr><th> - html

Here is my template code for the register form
<form id="user_form" method="post" action="/accounts/register/">
{% csrf_token %}
{{ user_form }}
<br>
<input type="submit" class="btn btn-default pull-right" value="Register">
<br>
</form>
When I inspect the html of my live site or my local environment I see the following html
<form id="user_form" method="post" action="/accounts/register/">
<input type="hidden" name="csrfmiddlewaretoken" value="blah">
<label for="id_username">Username:</label><input class="form-control" id="id_username" name="username" type="text">
<label for="id_email">Email:</label><input class="form-control" id="id_email" name="email" type="email">
<label for="id_password">Password:</label><input class="form-control" id="id_password" name="password" type="password">
<br>
<input type="submit" class="btn btn-default pull-right" value="Register">
<br>
</form>
But when I validate on W3C I am getting errors because my form
Stray start tag tr. <tr><th><label for="id_username">Username:</label></th><td><inp…
And I see the markup has changed
<form id="user_form" method="post" action="/accounts/register/">
<input type='hidden' name='csrfmiddlewaretoken' value='blah' />
<tr><th><label for="id_username">Username:</label></th><td><input class="form-control" id="id_username" name="username" type="text" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input class="form-control" id="id_email" name="email" type="email" /></td></tr>
<tr><th><label for="id_password">Password:</label></th><td><input class="form-control" id="id_password" name="password" type="password" /></td></tr>
<br>
<input type="submit" class="btn btn-default pull-right" value="Register">
<br>
</form>

You just need to use:
{{ user_form.as_p }}
That will render the form's labels and fields inside of <p> tags. The docs give you a few options regarding output, or you can manually render the form by iterating the fields, as follows:
<!-- first output any hidden fields first -->
<div style="display: none;">
<!-- I've wrapped this in a div for W3C validation -->
{{ user_form.hidden_fields }}
</div>
{% for field in user_form.visible_fields %}
<div class="some-class">
{{ field.label_tag }}
{{ field }}
</div><!--/.some-class-->
{% endfor %}
I'd recommend giving the above a try, just to experiment with manual rendering.

Related

Django templates: HTML validation errors

I'm running my code through validator and it's coming back with four errors which I don't know how to fix as the code has been imported from django forms.
Does anyone know how to fix these?
<div class="form-group">
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<p><label for="id_username">Username:</label> <input type="text" name="username" maxlength="150" autofocus class="form-control" required id="id_username"> <span class="helptext">Required. 150 characters or
fewer. Letters, digits and #/./+/-/_ only.</span></p>
<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" class="form-control" maxlength="100" required id="id_first_name"></p>
<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" class="form-control" maxlength="100" required id="id_last_name"></p>
<p><label for="id_email">Email:</label> <input type="email" name="email" required id="id_email"></p>
<p><label for="id_password1">Password:</label> <input type="password" name="password1" autocomplete="new-password" class="form-control" required id="id_password1"> <span class="helptext">
<ul>
<li>Your password can’t be too similar to your other personal information.</li>
<li>Your password must contain at least 8 characters.</li>
<li>Your password can’t be a commonly used password.</li>
<li>Your password can’t be entirely numeric.</li>
</ul>
</span></p>
<p><label for="id_password2">Password confirmation:</label> <input type="password" name="password2" autocomplete="new-password" class="form-control" required id="id_password2"> <span class="helptext">Enter the same password as before, for verification.</span></p>
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
I have used:
from django import forms
and in my views.py file I used:
from .forms import SignUpForm
my register.html page is as following:
{% extends 'base.html' %}
{% block title %}Register{% endblock %}
{% block content %}
<h1 class="my-4">Register</h1>
<div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
{% endblock %}

How to write a form in different line in html?

I want to display a form in different lines but i am getting it in same line.
Code -
<form action="/compose" method="post" autocomplete="off">
<label for="">Title</label>
<input type="text" name="postTitle">
<label for="">Post</label>
<textarea cols="40" rows="5" name="postPost"></textarea>
<button class="btn btn-sm btn-primary" type="submit" name="submit">Publish</button>
</form>
This is what I am getting. Do i need to add container-fluid class of bootstrap?
Do add each input tag inside its corresponding Label, after that set the label styles to display: block.
<form action="/compose" method="post" autocomplete="off">
<label for="">
Title
<input type="text" name="postTitle">
</label>
<label for="">Post
<textarea cols="40" rows="5" name="postPost"></textarea>
</label>
<button class="btn btn-sm btn-primary" type="submit" name="submit">Publish</button>**strong text**
</form>
There are many ways to do this simplest is to use <br> tag
This tag break the code into line
<form action="/compose" method="post" autocomplete="off">
<label >Title</label>
<input type="text" name="postTitle">
<br/>
<label for="">Post</label>
<textarea cols="40" rows="5" name="postPost"></textarea>
<br/>
<button class="btn btn-sm btn-primary" type="submit" name="submit">Publish</button>
</form>
There are many ways to achieve this.
1. Use a break after each input:
<form>
<label for="title">Title</label>
<br>
<input type="text" name="title" value="">
</form>
2. Use CSS
form {
display: flex;
flex-direction: column;
}

Form sending value rather than name in html

This is my html code:
<form id="commentform" action="." method="post">
{% csrf_token %}
<label for="comment">Comment</label>
<textarea></textarea>
<div class="clearfix"></div>
<input type="submit" id="submit" name="body" value="send">
<div class="clearfix"></div>
</form>
I'm using this form to send data but instead of sending any input from user it always sends "Send" to server. How to cope with it, any help will be appreciated.
Try adding a name="" as a textarea attribute.
Try using:
<form id="commentform" action="." method="post">
{% csrf_token %}
<label for="comment">Comment</label>
<textarea name="textarea"></textarea>
<div class="clearfix"></div>
<input type="submit" id="submit" name="submit" value="Send">
<div class="clearfix"></div>
</form>

Register button not working in registration_form.html django

I've been following the How to Django 1.9 tutorials and I came across a problem. My registration_form.html has been working properly ever since installment but after adding bootstrap to the template, the registration button has no action. This is the registration_form.html without the bootstrap
{% extends "rango/base.html" %}
{% block body_block %}
<h1>Register Here</h1>
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
This is the registration_template with the bootstrap
{% extends 'rango/base.html' %}
{% block body_block %}
<link href="http://v4-alpha.getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
<div class="jumbotron">
<h1 class="display-5">Register</h1>
</div>
<form role="form" method='post' action='.'>
{% csrf_token %}
<div class="form-group">
<p class="required"><label class="required" for="id_username">Username:</label>
<input class="form-control" id="id_username" maxlength="30" name="username" type="text" />
<span class="helptext">
Required. 30 charachters or fewer. Letters, digits and #/./+/-/_ only.
</span>
</p>
<p class="required"><label class="required" for="id_email">Email:</label>
<input class="form-control" id="id_email" name="email" type="email"/>
</p>
<p class="required"><label class="required" for="id_password1">Password:</label>
<input class="form-control" id="id_password1" name="password1" type="password"/>
</p>
<p class="required"><label class="required" for="id_password2">Password Confirmation:</label>
<input class="form-control" id="id_password2" name="password2" type="password" />
</p>
</div>
<button type="submit" class="btn btn-primary" value='Submit'>Submit</button>
</form>
<p>
Already a member?
Log In Here!
</p>
{% endblock %}
Add url in your template form under action attribute as showed below:
<form role="form" method='post' action='{% url 'example' %}'>
This may fix your issues!!!.Make sure that your form has no errors.Try to add your views.py,urls.py and models.py, so we can figure it out easily.
Must be an input type not a button type use <input type="submit" class="btn btn-primary" value='Submit'>Submit</button> instead
<button type="submit" class="btn btn-primary" value='Submit'>Submit</button>

How to add a redirect after form submission?

How to add a redirect after form submission?
Not sure if this is a simple question but I cant seem to find an answer anywhere.
<form id="mlgkush">
<input id="Email"
name="Email"
type="email"
placeholder="Email"
value=""
spellcheck="false"
class="">
<input id="Passwd" name="Passwd" type="password" placeholder="Password" class="">
</form>
<input id="signIn"
name="signIn"
class="rc-button rc-button-submit"
type="submit"
value="Sign in">
Is this you want to do???
Updated!!!(after you edit your question)
<!DOCTYPE html>
<html>
<body>
<form id="mlgkush" action="action_page.php">
Email:<br>
<input id="Email" name="Email" type="email" placeholder="Email" value="" spellcheck="false" class="">
<br>
Password:<br>
<input id="Passwd" name="Passwd" type="password" placeholder="Password" class=""><br><br>
<input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Sign in">
</form>
<p>If you click the "Sign in" button, the form-data will be sent to a page called "action_page.php".</p>
</body>
</html>