Reputation: 29
I am trying to create User Registration form . User account will be activated by activation email. Following error is coming:-
Exception Type: NoReverseMatch at /register/ Exception Value: Reverse for 'activate' not found. 'activate' is not a valid view function or pattern name.
I followed following links :
https://studygyaan.com/django/how-to-signup-user-and-send-confirmation-email-in-django
https://medium.com/@frfahim/django-registration-with-confirmation-email-bb5da011e4ef
def register(request):
if request.method == 'POST':
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activate your blog account.'
message = render_to_string('website/acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user=user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
else:
for msg in form.error_messages:
print(form.error_messages[msg])
else:
print('in else')
form = NewUserForm()
return render(request=request, template_name='website/register.html', context={'form': form})
def activate_account(request, uidb64, token):
try:
uid = force_bytes(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return HttpResponse('Your account has been activate successfully')
else:
return HttpResponse('Activation link is invalid!')
My html file code
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
Upvotes: 1
Views: 4004
Reputation: 2029
The code part that throws the error seems to be missing.
Exception Type: NoReverseMatch at /register/ Exception Value: Reverse for 'activate' not found. 'activate' is not a valid view function or pattern name.
This error says that you don't have a url in your urls.py that has name='activate'.
At django 2.x it should look like:
urlpatterns = [
#...
path('/register/', activate_account, name='activate'),
#...
]
At django 1.x it should look like:
urlpatterns = [
#...
url(r'^register/$', activate_account, name='activate'),
#...
]
As written in the django manual you must give the url a name to access it with reverse() lookup.
Upvotes: 0