python - django url 中的正则表达式错误

标签 python regex django url parameters

我的 django url 不起作用,我收到 ReverseMatch 错误:

Reverse for 'activation_mail' with arguments '()' and keyword arguments '{u'activation_key': '1c38a44d216a51c26e65d789a3d8af2677cebebd', u'email': u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83eefae6eee2eaefc3e4eee2eaefade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>', 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>.*)/$']

用户注册网站时会发生此错误。输入验证码后,尝试向用户地址发送确认电子邮件时发生错误。

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'),
)

邮件是从触发模型中以下功能的 View 发送的...

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()

错误发生在用于邮件的模板中...

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>

我用过http://pythex.org/ ,正则表达式有效。 enter image description here 错误是什么?

最佳答案

命名组 syntax是不正确的。它应该以 ?:

开头
^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^

顺便说一句,我还认为最后一个通配符匹配应该是非贪婪(不确定这是否重要):

(?P<site_url>.*?)

关于python - django url 中的正则表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507303/

相关文章:

java - 正则表达式验证仅在固定长度之间接受的密码

regex - 引用-此正则表达式是什么意思?

c# - 在 C# 中用正则表达式替换 "new line"字符

Django allauth KeyError : u'facebook'

python - 如何查看在 Django 的 manage.py test 命令期间运行了哪些测试

python - 我正在尝试简化我的代码,但答案是错误的

python - 如何跟踪 csr 矩阵

python - 集成 python-social-auth (social-app-django) 以与 Django 管理一起使用

python - 从 URL 打开获取 JSON

python - 将扁平高斯模型拟合到给定数据