Django URL 以结尾

Django URL endswith

我在 Django 工作已经有一段时间了,我很生疏。我正在尝试创建一个新项目和应用程序,并且我复制并粘贴了以前(和正在运行的)应用程序中的一些代码,因此我不必从头开始输入所有内容,但在尝试时我不断收到以下错误运行服务器:

File "/home/jboucher/anaconda3/envs/test_pilot/lib/python3.6/site-packages/django/core/checks/urls.py", line 104, in check_url_settings if value and not value.endswith('/'):

AttributeError: 'tuple' object has no attribute 'endswith'

我知道我只是遗漏了一些简单的东西,但我似乎找不到它。这是我的代码,现在有很多未使用的代码被注释掉了:

项目网址:

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'', include('test_pilot.urls', namespace='test_pilot')),
    url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

应用程序网址:

from django.conf.urls import url

from . import views

app_name = 'test_pilot'

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
#    url(r'^accounts/login/$', views.LoginView.as_view(), name='login'),
#    url(r'^logout/$', views.LogoutView.as_view(), {'next_page': '/accounts/login'}, name='logout'),
]

应用views.py

from django.contrib.auth import REDIRECT_FIELD_NAME, logout as auth_logout
from django.contrib.auth.views import LoginView as AuthLoginView
from django.utils.http import is_safe_url
from django.views.generic import TemplateView, RedirectView
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required


#@method_decorator(login_required, name='test_pilot')
class IndexView(TemplateView):
    template_name = 'pages/index.html'

    def test_pilot(self, *args, **kwargs):
        return super(IndexView, self).dispatch(*args, **kwargs)


# class LoginView(AuthLoginView):
#     success_url = '/'
#     template_name = 'pages/login.html'
#     redirect_field_name = REDIRECT_FIELD_NAME
#
#     def get_success_url(self):
#         redirect_to = self.request.GET.get(self.redirect_field_name)
#         if not is_safe_url(url=redirect_to, host=self.request.get_host()):
#             redirect_to = self.success_url
#         return redirect_to
#
#
# class LogoutView(RedirectView):
#     """
#     Provides users the ability to logout
#     """
#     url = '/accounts/login/'
#
#     def get(self, request, *args, **kwargs):
#         auth_logout(request)
#         return super(LogoutView, self).get(request, *args, **kwargs)

我的模板现在只是一个简单的 hello world html 页面。再一次,我知道我遗漏了一些简单的东西,但是我已经有很长时间没有接触过任何 Django 编程了。

问题出在您的设置文件中:STATIC_URLMEDIA_URL 之一有尾随逗号,这会将其变成元组。