I have recently started to use django-rest-framework in my projects and I have faced a problem. Here is a simple auth form I have:
<form action="{% url 'rest_framework:login' %}" method="post">
{% csrf_token %}
<input type="e-mail" name="username" value="" placeholder="Логин" maxlength="100" required="required">
<input type="password" name="password" value="" placeholder="Пароль" maxlength="100" required="required">
<button type="submit" class="lightbluebtn">Войти</button>
</form>
I can't really figure out how to validate it. I just need the error to be shown on the form. Every time I submit my form with invalid data I redirected to rest-framework login page. Also I have django-rest-framework default urls:
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
Is there any way to use it with django default forms?
Can you guys help me to fix it? I will be very grateful for the help.
if for the api you are using a session authentication then you don't need the rest_framework login page, that page is useful usually in the dev environment, you can just point to the {% url 'login' %}
page (that uses django.contrib.auth.views.login view), and override that template by naming yours registration/login.html
, for outputting form errors just use {{ form.error }}
, like for example:
{# file registration/login.html #}
{% if form.errors %}
<div class="alert alert-danger">
<p>Your email and password didn't match. Please try again.</p>
</div>
{% endif %}
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
<input type="e-mail" name="username" value="" placeholder="Логин" maxlength="100" required="required">
<input type="password" name="password" value="" placeholder="Пароль" maxlength="100" required="required">
<button type="submit" class="lightbluebtn">Войти</button>
</form>