puput icon indicating copy to clipboard operation
puput copied to clipboard

no_reverse_match error where django username is an email address

Open thclark opened this issue 5 years ago • 3 comments

Summary

If the django user model is modified such that the email address is treated as a username, the entries_by_author route cannot find a reverse match, resulting in server error.

Here are the places where entries_by_author are defined and used in puput.

Error details

Here is how to do django auth with an email username, if you wish to reproduce the error. Alternatively it might be possible to reproduce by setting an email address as a django username.

The abridged error is as follows:

ERROR 2019-06-13 13:11:12,433 log 98761 123145480708096 Internal Server Error: /blog/
Traceback (most recent call last):

[ ... blah massive stacktrace ... ]

  File "/Users/thc29/.pyenv/versions/tpast/lib/python3.6/site-packages/wagtail/contrib/routable_page/templatetags/wagtailroutablepage_tags.py", line 22, in routablepageurl
    routed_url = page.reverse_subpage(url_name, args=args, kwargs=kwargs)
  File "/Users/thc29/.pyenv/versions/tpast/lib/python3.6/site-packages/wagtail/contrib/routable_page/models.py", line 85, in reverse_subpage
    return self.get_resolver().reverse(name, *args, **kwargs)
  File "/Users/thc29/.pyenv/versions/tpast/lib/python3.6/site-packages/django/urls/resolvers.py", line 555, in reverse
    return self._reverse_with_prefix(lookup_view, '', *args, **kwargs)
  File "/Users/thc29/.pyenv/versions/tpast/lib/python3.6/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'entries_by_author' with arguments '('[email protected]',)' not found. 1 pattern(s) tried: ['author/(?P<author>\\w+)/$']
[13/Jun/2019 13:11:12] "GET /blog/ HTTP/1.1" 500 234407

Not sure about this

I put the pattern author/(?P<author>\\w+)/$ into regex101 and it doesn't match any of the possible inputs I can think of - usernames, email addresses, or tuples as set in this argument (like ('[email protected]',)). So not sure how this works for anybody else either?

Workaround

Override the entry_links.html template (I copied it from puput/templates to myapp/templates/puput) to remove the entries by author link:

{% load wagtailroutablepage_tags puput_tags %}

<ul class="links">
{#    <li>#}
{#        <i class="fa fa-user"></i>#}
{#        <a href="{% routablepageurl blog_page 'entries_by_author' entry.owner.username %}">#}
{#            {{ entry.owner.username }}#}
{#        </a>#}
{#    </li>#}
    <li>
        <i class="fa fa-calendar"></i>
        {{ entry.date|date:"DATE_FORMAT" }}
    </li>
    {% if entry.categories.count > 0 %}
        <li>
            <i class="fa fa-folder-open"></i>
            {% categories_list entry.categories %}
        </li>
    {% endif %}
    {% if entry.tags.count > 0 %}
        <li>
            <i class="fa fa-tag"></i>
            {% tags_list blog_page.num_tags_entry_header entry.tags %}
        </li>
    {% endif %}
    {% if blog_page.display_comments %}
        <li>
            <i class="fa fa-comments"></i>
            {{ entry.num_comments }}
        </li>
    {% endif %}
</ul>

Long Term Solution

Probably the best solution is to adjust the route as per the accepted answer here, since an invalid username in the URL shouldn't bring the server to its knees: https://stackoverflow.com/questions/32395062/django-urlpattern-for-username

thclark avatar Jun 13 '19 13:06 thclark

Take a look at this: https://github.com/APSL/puput/issues/122

marctc avatar Jun 13 '19 13:06 marctc

Yup, v. similar issue

thclark avatar Jun 13 '19 18:06 thclark

for anyone still having this issue, this comment helped me resolve: https://github.com/APSL/puput/issues/160#issuecomment-382530615

sodevious avatar Jan 19 '24 19:01 sodevious