2

My django url is not working, I get a ReverseMatch error:

Reverse for 'activation_mail' with arguments '()' and keyword arguments '{u'activation_key': '1c38a44d216a51c26e65d789a3d8af2677cebebd', u'email': u'[email protected]', u'site_url': 'http://127.0.0.1:8000'}' not found. 1 pattern(s) tried: [u'accounts/mails/(P<activation_key>[0-9a-zA-Z]{40})/(P<email>[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.][a-zA-Z0-9-.]+)/(P<site_url>.*)/$']

The error happens when a user registers to the website. After entering a captcha code, the error occurs when trying to send a confirmation email to the user address.

urls.py:

urlpatterns = patterns(
    url(r'^mails/(P<activation_key>[0-9a-zA-Z]{40})/(P<email>[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.][a-zA-Z0-9-.]+)/(P<site_url>.*)/$', TemplateView.as_view(template_name='accounts/mails/activation_email.html'), name='activation_mail'),
)

The mail is sent from the view that triggers the following function in the model...

models.py:

  class SignupProfile(models.Model):
    objects = SignupManager()
    user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True,
                             verbose_name=_("utilisateur"))
    activation_key = models.CharField(_("clef d'activation"), max_length=40)

    def send_activation_email(self, request):
        activation_url = reverse(
            'accounts:activation',
            kwargs={'activation_key': self.activation_key})

        ctx = Context({'site_url': settings.SITE_URL,
                       'activation_key': self.activation_key,
                       'email': self.user.email})

        html_tpl = get_template('accounts/mails/activation_email.html')
        html_content = html_tpl.render(ctx)
        msg = EmailMultiAlternatives(subject, text_content,
                                     settings.DEFAULT_FROM_EMAIL,
                                     [self.user.email])
        msg.attach_alternative(html_content, 'text/html')
        msg.send()

The error happens in the template used for the mail...

index.html:

            <a href="{% url 'accounts:activation_mail' activation_key=activation_key email=email site_url=site_url %}" target="_blank">View this email in your browser</a>

I have used http://pythex.org/, the regex works. enter image description here What's the error?

2
  • I'm a little confused by the set up: the single function in views.py isn't a view, it's logic to send email. Is that the entirety of your main urls.py file? It should be setting up a urlpatterns variable to hold those urls. It's not possible to answer completely without seeing the values you are using for the keyword variables. Commented Apr 8, 2016 at 19:05
  • I just edited my post with more details. Commented Apr 8, 2016 at 19:19

2 Answers 2

3

The named group syntax is not correct. It should start with ?:

^mails/(?P<activation_key>[0-9a-zA-Z]{40})/(?P<email>[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[.][a-zA-Z0-9-.]+)/(?P<site_url>.*)/$
    HERE^                               HERE^                                                      HERE^

As a side note, I also think the last wildcard match should be non-greedy (not sure if this matters):

(?P<site_url>.*?)
Sign up to request clarification or add additional context in comments.

Comments

1

try to use this rule, because the name group must start with ?

    r'^mails/(?P<activation_key>[0-9a-zA-Z]{40})/(?P<email>[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)/(?P<site_url>.*)/$'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.