0

I have read similar questions but I cannot understand why this is not working. I am trying to send a reactivation email to user when they click on a link. The activation link is generated properly when the user signs up and the email is sent, but when I try to call the same function again to reactivate link, it is not working saying it is missing one parameter. Here's the function:

acounts/views.py

def sendactivationmail(request, user):
    # Creating token and masing uid of user
    token = default_token_generator.make_token(user)
    uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
    # Sending email for email verification
    subject = 'Please Verify Your Email'
    from_email = settings.DEFAULT_FROM_EMAIL
    to_email = [user.email]
    context = {
        'url': settings.BASE_URL + reverse('user-activation-link', kwargs={
            'uidb64': uid,
            'token': token,
        }),
    }
    contact_message = render_to_string('verify_email.html', context)
    plain_message = strip_tags(contact_message)
    send_mail(subject, plain_message, from_email, to_email, html_message=contact_message, fail_silently=True)

    return redirect(reverse('login'))

accounts/urls.py

from django.conf.urls import url
from .views import *
from . import views
from urllib import request
from django.contrib.auth.models import User


urlpatterns = [
    url(r'^reactivate/$', views.sendactivationmail(request, User), name='reactivate'),

Is this the right way to pass on the request and user parameters to the function?

EDIT: This is the link that is used to redirect user:

<p><a href="{% url 'reactivate' %}">Click here</a> to resend the activation email.</p>
Naeem Khan
  • 950
  • 4
  • 13
  • 34
  • please post the html where the user is clicking to send activation link – Exprator Jun 20 '19 at 06:54
  • 1
    you are passing User model, from User model get the particular user and then pass that user object as parameter. – Vikas Periyadath Jun 20 '19 at 06:54
  • @Exprator I have updated the question with it, but it is a simple link that redirects to reactivate page. – Naeem Khan Jun 20 '19 at 07:05
  • @VikasP Could you please explain how I can do it? I have tried User.pk() and other objects of User class but I cannot seem to figure it out. – Naeem Khan Jun 20 '19 at 07:07
  • 1
    @NaeemKhan get user object like this , `user = User.objects.get(username="which ever user you want to send")` and pass this user object. – Vikas Periyadath Jun 20 '19 at 07:09
  • Is it possible to get the current logged in user? – Naeem Khan Jun 20 '19 at 07:11
  • I'm not sure why you would send an reactivation link to a logged in user but `user=request.user` would give the logged in user. – HenryM Jun 20 '19 at 07:15
  • @HenryM I have tried that and it gives: AttributeError: module 'urllib.request' has no attribute 'user' error. I have a custom login/signup system, and if the user has not activated their email address, they are shown a page where it says they must validate their email address. In such case, the is_active flag is set to false, and I want them to be able to resend the activation link. – Naeem Khan Jun 20 '19 at 07:17

1 Answers1

1

change your urlpattern to this

urlpatterns = [
    url(r'^reactivate/$', views.sendactivationmail, name='reactivate'),
    .....
]

and html to, but the user has to be logged in to use this link. remember that

<p><a href="{% url 'reactivate' request.user %}">Click here</a> to resend the activation email.</p>
Exprator
  • 26,992
  • 6
  • 47
  • 59