Forgot Password function, django - 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.

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

How to catch errors from form in 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

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>

Django registration and password reset on one page

Is there way to create just one page for login and reset password?
I tried to create two forms page with following code in template:
<form action="" method="post" id="formLoginIndex">
{% csrf_token %}
<div id="elogin">
<p><label for="id_username">Login</label>
{{ form.username }}</p>
<p><label for="id_password">Пароль</label>
{{ form.password }}</p>
</div>
<p class="submit">
<button type="submit" class="enter">Enter</button>
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% else %}
<input type="hidden" name="next" value="/electricity/" />
{% endif %}
<button class="forgot" type="button" onclick="$('#forgotten').toggle('normal');">Forgot password?</button>
</p>
</form>
<div id="forgotten">
<form action="/reset/done/" method="post" id="formForgot">
{% csrf_token %}
<p>
<label for="id_username_forgot">Login</label>
<input id="id_username_forgot" type="text" name="username" maxlength="30" />
</p>
<p>
<label for="id_email">e-mail</label>
<input id="id_email" type="text" name="email" maxlength="40" />
</p>
<p class="submit">
<button class="remember" type="submit">Reset</button>
</p>
</form>
</div>
And urls :
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_done'),
However works only login feature. Reset doesn't work. Obviously is that I'm just doing smth wrong.
So should I load somehow views.password_reset into the same page or even rewrite django in auth views or there is another common solution?
The point is that you need a form to show the PasswordResetForm and it's action should be set to point to reset/
when this form is submitted via method POST, it's redirected to where the argument post_change_redirect points to, and there you can show the user that the password has been changed.
( In case the form evaluates to be correct , else, it would re-render the form with errors shown )
url(r'^reset/$', password_reset,
{'template_name':'your_template',
'post_change_redirect':'/reset/done/',
'extra_context':{'argument':'to tempate'}}, name='some_name'),
url(r'^reset/done/', password_reset_done,
{'template_name':'the template to show a success message',
'extra_context':{'message':'your password is changed successfully'}},),
looking at the defaults for this function ,It gives some cool info about how it works and its defaults if not given:
password_reset (request, is_admin_site=False,
template_name='registration/password_reset_form.html',
email_template_name='registration/password_reset_email.html',
subject_template_name='registration/password_reset_subject.txt',
password_reset_form=PasswordResetForm,
token_generator=default_token_generator,
post_reset_redirect=None,
from_email=None,
current_app=None,
extra_context=None)