Django templates: HTML validation errors - html

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

Related

Django change password

Trying to change password using PasswordChangeView, but cannot get it working.
urls.py
from django.contrib.auth import views as auth_views
urlpatterns = [
path('profiles/settings/', update_profile, name='update_profile'),
path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'),
name='password_change'),
]
And i am trying to get the input fields correct in my html
<div class="tab-pane fade" role="tabpanel" id="password">
<form id="id_password_change_form" method="POST" class="form-signin">{% csrf_token %}
<div class="form-group row align-items-center">
<label class="col-3">Current Password</label>
<div class="col">
<input
type="password"
placeholder="Enter your current password"
name="old_password"
class="form-control"
id="id_old_password"
required="true" />
</div>
</div>
<div class="form-group row align-items-center">
<label class="col-3">New Password</label>
<div class="col">
<input
type="password"
placeholder="Enter a new password"
name="new_password1"
class="form-control"
id="id_new_password1"
required="true" />
<small>Password must be at least 8 characters long</small>
</div>
</div>
<div class="form-group row align-items-center">
<label class="col-3">Confirm Password</label>
<div class="col">
<input
type="password"
placeholder="Confirm your new password"
name="new_password2"
class="form-control"
id="id_new_password2"
required="true" />
</div>
</div>
{% for field in form %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary">Change Password</button>
</div>
</form>
There is no error, and it do not update the password as supposed to. according to PasswordChangeView, I should not need to alter anything.
I think you have a problem because of the wrong URLs definition, take a look at you urls.py:
from django.contrib.auth import views as auth_views
urlpatterns = [
path('profiles/settings/', update_profile, name='update_profile'),
path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'),
name='password_change'),
]
You always hit update_profile view instead of PasswordChangeView. I think this is typo.

How should I save to the database?

I have created a Customer model in Django and am not able to save the data to the database table and when I click the signup button it just redirects me back to the home page.
I have attached below the customer model and the signup function.
...please help me resolve the issue.
customer.py
from django.db import models
class Customer(models.Model):
first_name= models.CharField(max_length=50)
last_name= models.CharField(max_length=50)
phone=models.CharField(max_length=15)
email=models.EmailField()
password=models.CharField(max_length=500)
def register(self):
self.save()
views.py
def signup(request):
if request.method == 'GET':
return render(request, 'signup.html')
else:
postData=request.POST
first_name=postData.get('firstname')
last_name=postData.get('lastname')
phone=postData.get('phone')
email=postData.get('email')
password=postData.get('password')
print(first_name,last_name,phone,email,password)
customer=Customer(first_name=first_name,last_name=last_name,phone=phone,email=email,password=password)
customer.register()
return HttpResponse("Signup success")
signup.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="p-4 m-4">
<div class="col-lg-5 mx-auto border rounded pt-4">
<h3 class="alert alert-light border rounded" >Create An Account</h3>
<form action="/" method="POST">
{% csrf_token %}
<!--firstname-->
<div class="form-group">
<label for="">First Name</label>
<input type="text" name="firstname" id="" class="form-control form-control-sm" placeholder="Mike">
</div>
<!--lastname-->
<div class="form-group">
<label for="">Last Name</label>
<input type="text" name="lastname" id="" class="form-control form-control-sm" placeholder="Ross">
</div>
<!--phone-->
<div class="form-group">
<label for="">Phone No</label>
<input type="text" name="phone" id="" class="form-control form-control-sm" placeholder="9876543210">
</div>
<!--email-->
<div class="form-group">
<label for="">Email</label>
<input type="email" name="email" id="" class="form-control form-control-sm" placeholder="abc#gmail.com">
</div>
<!--password-->
<div class="form-group">
<label for="">Password</label>
<input type="password" name="password" id="" class="form-control form-control-sm" placeholder="********">
</div>
<div class="form-group">
<input class="btn btn-sm btn-info" type="submit" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
{% endblock %}
Try this change
Model
def register(self):
return self.save()

I am getting an error in django project while submitting the form can any one please tell the correction of my error

Error
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/contact/contact
Using the URLconf defined in portfolio.urls
admin/
[name='home']
about/ [name='about']
projects/ [name='projects']
contact/ [name='contact']
The current path, contact/contact, didn't match any of these.
**code for the form **
<form action="contact" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Your Name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="name#example.com">
</div>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Your Number">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Explain your concern</label>
<textarea class="form-control" id="desc" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
views.py
def contact(request):
return render(request,"contact.html")
urls.py
this url is from my app
urlpatterns=[
path("",views.home,name="home"),
path("about/",views.about,name="about"),
path("projects/",views.projects,name="projects"),
path("contact/",views.contact,name="contact"),
]
urls.py
this url is from my project
urlpatterns = [
path('admin/', admin.site.urls),
path("",include("home.urls")),
]
The url should propbaly be: http://localhost:8000/contact/
So change it to blank so will it from current which is a GET request to a POST
request of the current url:
action=""
Propably better to use action="{% url 'contact' %}"
which is your named route contact.

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>

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

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.