Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoReverseMatch with custom url #83

Open
jslovern391 opened this issue Aug 31, 2023 · 12 comments
Open

NoReverseMatch with custom url #83

jslovern391 opened this issue Aug 31, 2023 · 12 comments

Comments

@jslovern391
Copy link

I was curious if anyone is looking at this because I have an issue.

@jslovern391 jslovern391 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
@LeoneBacciu
Copy link
Owner

Yes, I recently started maintaining this project again.
What issue do you have?

@LeoneBacciu LeoneBacciu reopened this Sep 1, 2023
@jslovern391
Copy link
Author

When using this it marks the user as active, but that is the page it goes to afterward.

\Python\Python311\Lib\site-packages\verify_email\views.py, line 58, in verify_user_and_activate

'link': reverse(login_page) ^^^^^^^^^^^^^^^^^^^

@jslovern391
Copy link
Author

Screenshot_20230901_171426_GitHub.jpg

@jslovern391
Copy link
Author

By the way, this is a really nice app

@LeoneBacciu
Copy link
Owner

I'm sorry but I don't understand the issue, could you be more specific?

@jslovern391
Copy link
Author

When the user clicks the verification link, that is the page that displays. I am assuming it should be the successful page.

@LeoneBacciu
Copy link
Owner

I'm guessing it might be a conflict with the app verify_email, can you share your urls configuration?

@jslovern391
Copy link
Author

Yes when I get home tonight

@jslovern391
Copy link
Author

I sent a verification email and then redirected to http://127.0.0.1:8000/accounts/login/. The user is created and is set to inactive. When the user clicks the verification email they are marked active and this page is shown to them.

NoReverseMatch at /verification/user/verify-email/anNsb3Zlcm5AZ21haWwuY29t/YnR3YWd4LWQ2ZGEwNGY4YWRkYzY4MDljNmIxMjE4NDEwZTE5NTMzOjFxY1JjbjpIcWRXNzFwclEwUW1fNnB3NE5MTUdsTmVsNFBQcmVmZjg4bnhkdFdYQmtF/

Reverse for '/accounts/login/' not found. '/accounts/login/' is not a valid view function or pattern name.
Request Method: GET
http://127.0.0.1:8000/verification/user/verify-email/anNsb3Zlcm5AZ21haWwuY29t/YnR3YWd4LWQ2ZGEwNGY4YWRkYzY4MDljNmIxMjE4NDEwZTE5NTMzOjFxY1JjbjpIcWRXNzFwclEwUW1fNnB3NE5MTUdsTmVsNFBQcmVmZjg4bnhkdFdYQmtF/
4.2.4
NoReverseMatch
Reverse for '/accounts/login/' not found. '/accounts/login/' is not a valid view function or pattern name.
C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py, line 828, in _reverse_with_prefix
verify_email.views.verify_user_and_activate
C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\python.exe
3.11.4
['D:\M28t\Django Project\m28t', 'C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\python311.zip', 'C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\DLLs', 'C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib', 'C:\Users\rtbr1\AppData\Local\Programs\Python\Python311', 'C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages']
Sat, 02 Sep 2023 14:29:43 +0000

Traceback Switch to copy-and-paste view

        <li class="frame django">
          
            <code class="fname">C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\exception.py</code>, line 55, in inner
          
    
          
            <div class="context" id="c2460442941440">
              
                
              
              <ol start="55" class="context-line">
                <li onclick="toggle('pre2460442941440', 'post2460442941440')"><pre>                response = get_response(request)
                               ^^^^^^^^^^^^^^^^^^^^^</pre> <span>…</span></li>
              </ol>
              
                
              
            </div>
          
    
          
            
              <details>
                <summary class="commands">Local vars</summary>
            
            
            </details>
          
        </li>
      
        
        <li class="frame django">
          
            <code class="fname">C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\handlers\base.py</code>, line 197, in _get_response
          
    
          
            <div class="context" id="c2460442949504">
              
                
              
              <ol start="197" class="context-line">
                <li onclick="toggle('pre2460442949504', 'post2460442949504')"><pre>                response = wrapped_callback(request, *callback_args, **callback_kwargs)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</pre> <span>…</span></li>
              </ol>
              
                
              
            </div>
          
    
          
            
              <details>
                <summary class="commands">Local vars</summary>
            
            
            </details>
          
        </li>
      
        
        <li class="frame user">
          
            <code class="fname">C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\verify_email\views.py</code>, line 58, in verify_user_and_activate
          
    
          
            <div class="context" id="c2460446110464">
              
                
              
              <ol start="58" class="context-line">
                <li onclick="toggle('pre2460446110464', 'post2460446110464')"><pre>                    'link': reverse(login_page)
                                ^^^^^^^^^^^^^^^^^^^</pre> <span>…</span></li>
              </ol>
              
                
              
            </div>
          
    
          
            
              <details>
                <summary class="commands">Local vars</summary>
            
            
            </details>
          
        </li>
      
        
        <li class="frame django">
          
            <code class="fname">C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\base.py</code>, line 88, in reverse
          
    
          
            <div class="context" id="c2460446138624">
              
                
              
              <ol start="88" class="context-line">
                <li onclick="toggle('pre2460446138624', 'post2460446138624')"><pre>    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</pre> <span>…</span></li>
              </ol>
              
                
              
            </div>
          
    
          
            
              <details>
                <summary class="commands">Local vars</summary>
            
            
            </details>
          
        </li>
      
        
        <li class="frame django">
          
            <code class="fname">C:\Users\rtbr1\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py</code>, line 828, in _reverse_with_prefix
          
    
          
            <div class="context" id="c2460441364032">
              
                
              
              <ol start="828" class="context-line">
                <li onclick="toggle('pre2460441364032', 'post2460441364032')"><pre>        raise NoReverseMatch(msg)
             ^^^^^^^^^^^^^^^^^^^^^^^^^</pre> <span>…</span></li>
              </ol>
              
            </div>
          
    
          
            
              <details>
                <summary class="commands">Local vars</summary></details></li></ul></div></div><!--EndFragment-->
    

    ####################################################################
    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    ############################################
    'member.apps.MemberConfig',
    'verify_email.apps.VerifyEmailConfig',
    'crispy_bootstrap4',
    'crispy_forms',
    

    urlpatterns = [
    path('', views.index, name='index'),
    path('member/int:pk/', views.MemberDetail.as_view(),name='member_detail'),
    path('create_org/', views.OrgCreate.as_view(),name='create_org'),
    path('login/', views.login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('signup/', views.signup, name='signup'),
    ]

    views.py
    def signup(request):
    if request.method == 'POST':
    form = MemberForm(request.POST)
    if form.is_valid():
    inactive_user = send_verification_email(request, form)
    form.cleaned_data['email']
    username = form.cleaned_data.get('username')
    email = form.cleaned_data.get('email')
    ######################## mail system ####################################
    # htmly = get_template('fish/Email.html')
    # d = { 'username': username }
    # subject, from_email, to = 'welcome', '[email protected]', email
    # html_content = htmly.render(d)
    # msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
    # msg.attach_alternative(html_content, "text/html")
    # msg.send()
    ##################################################################
    messages.success(request, f'Verification email has been sent. Please check spam folder.')
    return redirect('login')
    else:
    form = MemberForm()
    return render(request, 'member/signup.html', {'form': form, 'title':'register here'})

@jslovern391
Copy link
Author

Sorry wrong urls.py

urlpatterns = [
path('admin/', admin.site.urls),
#######################################
path('', RedirectView.as_view(url='member/signup')),
path('member/', include('member.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('verification/', include('verify_email.urls')),
]

@LeoneBacciu
Copy link
Owner

Ok, I understand better now. A couple of things:

  1. Are you sure you are using the latest version? v0.3.1
  2. Please, use the 'Add Code' on github, the error is extremely hard to read. You could also add the html file.
  3. It seems there is a problem with the resolution of the url, but you say that the user gets activated, are you sure?
  4. Have you followed the documentation here to add the token verification url?

@LeoneBacciu LeoneBacciu changed the title Is this app still being supported? NoReverseMatch with custom url Sep 2, 2023
@diesieben07
Copy link

I just stumbled upon this issue when trying to figure out why this library generated a wrong URL and I think it might be the same cause. I am specifying a custom URL for the view:

path('welcome/<str:token>/', verify)

This will generate URLs in the form of https://example.com/welcome/the-token-here. Note the missing trailing slash. The cause here is this piece of code, which does some ... interesting things to generate the URL:

d = [v[0][0] for k, v in get_resolver(None).reverse_dict.items() if has_decorator(k)]
d = [a[0][:a[0].index('%')] for a in d if len(a[1])]
if len(d) == 0:
logger.error(DJANGO_EMAIL_VERIFICATION_URL_ROUTE_ERROR)
return
if len(d) > 1:
logger.error(f'{DJANGO_EMAIL_VERIFICATION_MORE_VIEWS_ERROR}: {d}')
return
if len(d) >= 1:
context['link'] = domain + d[0] + token

In particular it seems to always assume that the token comes at the end and there is no trailing slash (or anything else after the token, for that matter).

In my opinion:

  • There should be a setting to specify a named url
  • If it isn't specified, do the same search for the decorator that's done currently
  • In either case, use reverse to build the URL instead of manual concatenation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants