首页 > 解决方案 > 为什么 django urls 呈现错误的路径?

问题描述

我处于一个有线的情况,我有一个项目我现在正在工作。最近,当我单击网页上的登录按钮时,我开始遇到问题。以下是整个 URL 条目:

urlpatterns = [
    path('', home, name='home'),
    path('contact/', contact, name='contact'),
    path("login/", login_user, name='login'),
    path("logout/", logout_user, name='logout'),
    path("register/", register_user, name='register'),
    path("complete_registration/", userprofileview, name='complete_registration'),
    path('profile1/', profile_page, name='profile1'),
    path('thank_you/', thank_you, name='thank_you'),
    path('about/', about, name='about'),
    path('editUserProfile/', editUserProfile, name='editUserProfile'),
    path('pass_edit/', passwordChange, name='pass_edit'),

    path('reset-password/', auth_views.PasswordResetView.as_view(template_name='authenticate\\password_reset_form.html'
                                                                 ), name='reset-password'),

    path('reset-password/done/', auth_views.PasswordResetDoneView.as_view(
        template_name='authenticate\\password_reset_done.html'), name='password_reset_done'),

    path('reset-password-confirm/<uid64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
        template_name='authenticate\\password_reset_confirm.html'), name='password_reset_confirm'),


    path('reset-password/complete/', auth_views.PasswordResetCompleteView.as_view())

]

所以,问题是当我点击登陆页面上的“登录”按钮时,我看到的网址如下:

http://127.0.0.1:8000/web/login/

如果有人看到“登录” url 端点,我还没有那样配置它,我得到的错误如下:

TemplateDoesNotExist at /web/login/
registration/login.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/web/login/
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:    
registration/login.html
Exception Location: C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py in select_template, line 47
Python Executable:  C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.0

所以,如果我看到我的“登录”方法,这个错误让我很困惑,下面是来自 views.py 的方法

def login_user(request):  # Login Page
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)

        if user is not None:
            login(request, user)
            if UserProfile.objects.filter(user=user).values('country').count() == 0:
                return redirect('complete_registration')
            else:
                return redirect('/profile1')
        else:
            messages.error(request, f'error while login, please try again')
            return redirect('login')
    else:
        return render(request, 'authenticate\\login.html', {})

我可以说它以前工作过。我最近对我的项目所做的唯一更改是在 App 的 urls.py 中添加内置视图,用于密码重置电子邮件通知。就是这样,这些 UI 问题仅在此更改之后才开始发生。所以,我不确定这种变化是否与我面临的问题相关。

另一个问题是“注销”。当我点击注销时,页面被重定向到 Django 默认 UI 页面(附上截图)。但是我也写了一个特定的注销方法:

def logout_user(request):  # Contact Page
    logout(request)
    messages.success(request, f'logout successful')
    return redirect('login')

假设重定向到“登录”页面。

NOTE: if I manually remove "web" from the above url then it takes me to the login page.

项目地址.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('AUTHENTICATION.urls')),
    re_path(r'^web/', include('django.contrib.auth.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

追溯日志:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/reset-password/

Django Version: 3.0.8
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'AUTHENTICATION',
 'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\templates\registration\password_reset_email.html, error at line 6
   Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': 'OTg', 'token': '5iz-e1a9208c86666f175c8b'}' not found. 1 pattern(s) tried: ['reset\\-password\\-confirm/(?P<uid64>[^/]+)/(?P<token>[^/]+)/$']
   1 : {% load i18n %}{% autoescape off %}
   2 : {% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
   3 : 
   4 : {% trans "Please go to the following page and choose a new password:" %}
   5 : {% block reset_link %}
   6 : {{ protocol }}://{{ domain }} {% url 'password_reset_confirm' uidb64=uid token=token %} 
   7 : {% endblock %}
   8 : {% trans 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
   9 : 
   10 : {% trans "Thanks for using our site!" %}
   11 : 
   12 : {% blocktrans %}The {{ site_name }} team{% endblocktrans %}
   13 : 
   14 : {% endautoescape %}
   15 : 

Traceback (most recent call last):
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\views.py", line 222, in dispatch
    return super().dispatch(*args, **kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\views.py", line 235, in form_valid
    form.save(**opts)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\forms.py", line 324, in save
    user_email, html_email_template_name=html_email_template_name,
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\forms.py", line 265, in send_mail
    body = loader.render_to_string(email_template_name, context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 37, in render
    output = self.nodelist.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 53, in render
    result = self.nodelist.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 443, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /reset-password/
Exception Value: Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': 'OTg', 'token': '5iz-e1a9208c86666f175c8b'}' not found. 1 pattern(s) tried: ['reset\\-password\\-confirm/(?P<uid64>[^/]+)/(?P<token>[^/]+)/$']

标签: django

解决方案


推荐阅读