-2
Good evening...I’m trying to make the user password recovery through the ID...but when sending the form with the ID... this error occurs. someone could help me?
py.
app_name = 'usuarios'
urlpatterns = [
path('', views.dashboard, name="dashboard"),
path('entrar/', LoginView.as_view(template_name='usuarios/login.html'), name="login"),
path('saida/', LogoutView.as_view(), {'next_page':'core_home'}, name="logout"),
path('cadastre-se/', views.register, name="register"),
path('nova-senha/', views.password_reset, name="password_reset"),
path('confirmar-nova-senha/(?P<key>\w+)/$', views.password_reset_confirm, name="password_reset_confirm"),
path('editar/', views.edit, name="edit"),
path('editar-senha/', views.edit_password, name="password"),
]
form py.
class PasswordResetForm(forms.Form):
nrg = forms.CharField(label='RG')
def clean_nrg(self):
nrg = self.cleaned_data['nrg']
if User.objects.filter(nrg=nrg).exists():
return nrg
raise forms.ValidationError(
'Nenhum usuário encontrado com esse RG'
)
py views.
def password_reset(request):
template_name = 'usuarios/password_reset.html'
context = {}
form = PasswordResetForm(request.POST or None)
if form.is_valid():
form.save()
context['success'] = True
return redirect('usuarios:password_reset_confirm')
context['form'] = form
context['key'] = generate_hash_key(request.user.username) # adicionas esta linha para adicionar ao context
return render(request, template_name, context)
def password_reset_confirm(request, key):
template_name = 'usuarios/password_reset_confirm.html'
context = {}
reset = get_object_or_404(PasswordReset, key=key)
form = SetPasswordForm(user=reset.user, data=request.POST or None)
if form.is_valid():
form.save()
context['success'] = True
return redirect('usuarios:login')
context['form'] = form
return render(request, template_name, context)
password_reset.html
{% extends "alocar/login_base.html" %}
{% load crispy_forms_tags %}
{% block logado %}
<div class="loginBox">
<h2>Informe o teu RG</h2>
<form action="{% url 'usuarios:password_reset_confirm' key %}" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" name="" value="Enviar">
</form>
</div>
{% endblock logado %}
password_reset_confirm.html
{% extends "alocar/login_base.html" %}
{% load crispy_forms_tags %}
{% block title %}Alterar Senha | {{ block.super }}{% endblock title %}
{% block logado %}
<div class="loginBox color-text='white'">
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" name="" value="Confirmar">
</form>
</div>
{% endblock logado %}
error message
You have somewhere in the template {% url 'users:password_reset_confirm' %}?
– Ernesto Casanova
@Ernesto Casanova.. good evening...I have yes...I added the html print in the post...look again for kindness...
– ElvisDba
Hey, reset.key, where are you going in context? The error that it indicates is even because there is no reset.key, try placing between plicas a value that you know exists in place of reset.key, for example, you will see that there is no error. To resolve you need to add to the context you are only passing the form.
– Ernesto Casanova
@Ernesto Casanova...good evening .... I’m sorry but I didn’t understand anything..
– ElvisDba
Colocas {% url 'usuarios:password_reset_confirm' key %} in template, and password_reset_confirm(request, key): adds context['key'] = key. If you want me to put a full answer, put your html in text instead of image.
– Ernesto Casanova
@Ernesto Casanova.. good evening...I switched the image with the html code.. as directed.. thank you for the effort in helping me
– ElvisDba
@Ernesto Casanova.. good evening...I made the changes that Victor suggested to me... occurred a simpler error... PAGE NOT FOUND...I re-read the post...now I put all the code involved in the process... form, views, urls, html...look kindly...I’m running out of time.. I have to deliver this tcc
– ElvisDba
The problem is on this line, reset = get_object_or_404(Passwordreset, key=key), here does a get should not find your key, then returns 404. I have indicated before, what exactly do you do with the key obtained with generate_hash_key? The way I put it may not generate the one you stored, if it is.
– Ernesto Casanova
@Ernestocasanova...good evening, this key is to delete the old password of the user, it goes along with the request user to form...at the time of rendering the template to register the new password happens this error 404...
– ElvisDba
OK the key is to delete...e?.... the new generated is equal to the one that exists, did you read my previous comment well? The idea was to stop and think a little. If the new generated key is not equal to the one in db, get get_object_or_404(Passwordreset, key=key) will return a 404. I gave to understand now?
– Ernesto Casanova
@Ernestocasanova.... good morning....ok... then I will erase it...
– ElvisDba
@Ernestocasanova.. good evening ...I followed your advice and erased where you had told me.. now this error appears.... Passwordresetform Object has no attribute 'save'
– ElvisDba
@Ernestocasanova.. you know no easier example of implementing that not?
– ElvisDba
Viva, a password reset solution, I use to send a link with a token or key via email here’s an example https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html. Even if you can’t, to help you, I need access to your code, you didn’t share your models.py for example, ideal is to share all the code, lap running on my machine and I see the problem.
– Ernesto Casanova
You have here source code with this feature. https://github.com/wsvincent/django-auth-tutorial
– Ernesto Casanova
@Ernestocasanova...good night. Once again I thank you very much for your helpful help, I had already implemented recovery by email, however it was discarded, another alternative was requested...I will share with you the repository on github...https://github.com/Dbaelvis/usuariogr was testing this part of the recovery separately from the project.... once again I thank you for your help
– ElvisDba
I shared an update in my initial response, you just need to get the password reset working.
– Ernesto Casanova
@Ernestocasanova...good night...okay now it’s all right...I really appreciate your insistence in helping me......
– ElvisDba
Hello alive, I still helped. Take advantage and mark my answer as useful, I hope it will be useful for someone else. Hug.
– Ernesto Casanova
@Ernesto Casanova.. once again good night.... yes for sure, I have marked as useful.... everything right at the time of reset the password.... functioned legal...however if at the time of confirming the password...I put the password in field 2, other than field 1, form disappears, only the button is left
– ElvisDba
@Ernestocasanova.. good afternoon... POST Prevent object change in relationship .... I have a TCC to present Wednesday...since already grateful
– ElvisDba
Hi, I didn’t quite understand your question. Do you want to avoid editing an attribute? In models you can define Editable=False. Validate if that’s what you need.
– Ernesto Casanova