How to create login page in Django
1 ) Create login.html file inside templates file.
{% block content %}
{% if form.errors %}
<p>! Sorry your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<div>
LogIn !!
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}
2 ) Edit urls.py with.
url(r'^users/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
3 ) Edit view.py with.
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
4 ) Run page with. http://127.0.0.1:8000/users/login/
{% block content %}
{% if form.errors %}
<p>! Sorry your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<div>
LogIn !!
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}
2 ) Edit urls.py with.
url(r'^users/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
3 ) Edit view.py with.
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
4 ) Run page with. http://127.0.0.1:8000/users/login/
Comments
Post a Comment