How to catch errors from form in Django? - django

I have a collapse div with login/sign-up forms. How I can catch errors if they are and show them on page?
My form in html:
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<p><label for="id_username">Username:</label> <input
id="id_username" type="text" name="username" maxlength="30" /></p>
<p><label for="id_password">Password:</label> <input
type="password" name="password" id="id_password" /></p>
<input type="submit" value="Log in" />
<input type="hidden" name="next" value="{{ request.get_full_path }}"
/>
</form>

Dude use the inbuilt login, it offers you with encryption of password. the method your using is little messy i would recommend to go with django inbuilt authentication. here's the link,
https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html

Related

Django: insert multiple input text and file

I have recently tried to learn Django and I want to know it's possible to insert multiple input with this form structure to table database? if it's possible, can you tell me how it is? Thank you!!!
<form class="log-in" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<div>
<input type="text" name="title">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
What you are looking for are formsets: docs.djangoproject.com/en/3.0/topics/forms/formsets

add django csrf token to angular 5 form

im working on django-angular5 project i have a login form in angular how should i add csrf token to my form ? without what i get this error
i searched same topic but people are using angular js that will not help me
(i ng build angular app and im using static files)
Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.
it is my simple form for login in home.compoenent.html
<form method="post">
<div class="input-field col s6">
<input id="first_name" type="text" name="username" class="validate">
<label for="first_name">نام کاربری</label>
</div>
<div class="input-field col s6">
<input id="first_name" type="password" name="password" class="validate">
<label for="first_name">کلمه عبور</label>
</div>
<input type="submit" class="waves-effect waves-light btn" value="login"/>
<input type="hidden" name="next" value="{{ next }}"/>
</form>
in your form you should just add {% csrf_token %}
you'll have something like this :
<form method="post">
{% csrf_token %}
<div class="input-field col s6">
<input id="first_name" type="text" name="username" class="validate">
<label for="first_name">نام کاربری</label>
</div>
<div class="input-field col s6">
<input id="first_name" type="password" name="password" class="validate">
<label for="first_name">کلمه عبور</label>
</div>
<input type="submit" class="waves-effect waves-light btn" value="login"/>
<input type="hidden" name="next" value="{{ next }}"/>
</form>

Django url parsing error

OK,
I have two different views, both in the project site-wide area.
urls.py
url(r'^accounts/login/$', 'taxo.views.login'),
url(r'^accounts/invalid/$', 'taxo.views.invalid'),
...
taxo/views.py
def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)
def invalid(request):
return render_to_response('invalid.html',{'title':'invalid'})
templates/login.html
<form action="/accounts/auth/" method="post">{% csrf_token %}
<label for="username">User name</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
</form>
templates/invalid.html
<form style="float: right" action="accounts/login/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Login" class="search"/>
</form>
With the above code, I got Page not Found error
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/invalid/accounts/login/
Django parses the requested url as relative to the url of the current page. When I replaced the action with the {% url %} tag. I got a NoReverseMatch at /accounts/invalid/ error
How do I do this correctly?
Try this:
<form style="float: right" action="/accounts/login/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Login" class="search"/>
</form>
And here's the reason:
Request URL: http://127.0.0.1:8000/accounts/invalid/accounts/login/
$ at the end of regex means nothing's after slash:
url(r'^accounts/login/$', 'taxo.views.login', name='login'),
url(r'^accounts/invalid/$', 'taxo.views.invalid', name='invalid'),
therefore you may use those urls:
http://127.0.0.1:8000/accounts/login/
http://127.0.0.1:8000/accounts/invalid/
edit:
why one of your URLs in template redirects begins with slash and one without? Try this one:
<form style="float: right" action="{% url 'login' %}" method="post">

Django-rest-framework: rewrite default form validaiton

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>

Forgot Password function, django

I followed the code here:
http://drumcoder.co.uk/blog/2010/apr/09/django-reset-password/
as well as here:
http://shrenikp.webs.com/apps/blog/entries/show/7133721-implement-forgot-password-on-customer-ui-
and here:
http://blog.montylounge.com/2009/07/12/django-forgot-password/
This is the code I currently wish to work with and what I have it at currently
[base.html]
<form name="login_form" action="/login/" method="post" accept-charset="utf-8" style="display: inline">
{% csrf_token %}
Username: <input type="text" name="username" value="" /><br />
Password: <input type="password" name="password" value="" /><br />
<input type="submit" value="submit" value = "" id ="submit" />
<p>Forgot password?</p>
</form>
[url.py]
urlpatterns = patterns('',
url(r'^password_reset/$', 'django.contrib.auth.views.password_reset', name='password_reset'),
(r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
(r'^reset/(?P[0-9A-Za-z]+)-(?P.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
)
I get an error something like this:
unknown specifier: ?P[
this line error for some reason... (r'^reset/(?P[0-9A-Za-z]+)-(?P.+)/$'
any help?
You're going to need to change it to this:
(r'^reset/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$', 'django.contrib.auth.views.password_reset_confirm')
You need to add the tag {% csrf_token %} after your <form> mark-up in your template. Try that.