反向''带参数'()'和关键字参数'{}'找不到。尝试过0种模式:[]

时间:2016-05-20 11:15:34

标签: python django

当我尝试使用login中的django.contrib.auth.views函数时,我遇到了错误。我得到的错误是Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []。关于同一主题的SO也有类似的问题,但都与功能有关。在这里,在错误消息中,错误字符串没有提到让我困惑的任何函数。 以下是settings.py和urls.py文件。如果需要任何其他信息,请告诉我。

urls.py

    from django.conf.urls import patterns, include, url
    from django.conf import settings
    from django.conf.urls.static import static
    from django.contrib.auth import views as auth_views

    from django.contrib import admin
    admin.autodiscover()

    from bootcamp.core import views as core_views
    from bootcamp.authentication import views as bootcamp_auth_views
    from bootcamp.activities import views as activities_views
    from bootcamp.search import views as search_views

    urlpatterns = [
        url(r'^$', core_views.home, name='home'),
        url(r'^login', auth_views.login, {'template_name': 'core/cover.html'}, name='login'),
        url(r'^logout', auth_views.logout, {'next_page': '/'}, name='logout'),
        url(r'^signup/$', bootcamp_auth_views.signup, name='signup'),
        url(r'^settings/$', core_views.settings, name='settings'),
        url(r'^settings/picture/$', core_views.picture, name='picture'),
        url(r'^settings/upload_picture/$', core_views.upload_picture, name='upload_picture'),
        url(r'^settings/save_uploaded_picture/$', core_views.save_uploaded_picture, name='save_uploaded_picture'),
        url(r'^settings/password/$', core_views.password, name='password'),
        url(r'^network/$', core_views.network, name='network'),
        url(r'^feeds/', include('bootcamp.feeds.urls')),
        url(r'^questions/', include('bootcamp.questions.urls')),
        url(r'^articles/', include('bootcamp.articles.urls')),
        url(r'^messages/', include('bootcamp.messenger.urls')),
        url(r'^notifications/$', activities_views.notifications, name='notifications'),
        url(r'^notifications/last/$', activities_views.last_notifications, name='last_notifications'),
        url(r'^notifications/check/$', activities_views.check_notifications, name='check_notifications'),
        url(r'^search/$', search_views.search, name='search'),
        url(r'^(?P<username>[^/]+)/$', core_views.profile, name='profile'),
        url(r'^i18n/', include('django.conf.urls.i18n', namespace='i18n')),
        url(r'^admin/', include(admin.site.urls)),
    ]

settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',

    'bootcamp.activities',
    'bootcamp.articles',
    'bootcamp.authentication',
    'bootcamp.core',
    'bootcamp.feeds',
    'bootcamp.messenger',
    'bootcamp.questions',
    'bootcamp.search',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [PROJECT_DIR.child('templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug':DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

修改

添加模板core / cover.html

{% extends 'base.html' %}
{% load staticfiles %}
{% load i18n %}

{% block head %}
  <link href="{% static 'css/cover.css' %}" rel="stylesheet">
{% endblock head %}

{% block body %}
  <div class="cover">
    <h1 class="logo">muHUB</h1>
    {% if form.non_field_errors %}
      {% for error in form.non_field_errors %}
        <div class="alert alert-danger alert-dismissable">
          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
          {{ error }}
        </div>
      {% endfor %}
    {% endif %}
    <div class="login">
      <h2>{% trans 'Log in' %}</h2>
      <form method="post" action="{% url 'login' %}" role="form">
        {% csrf_token %}
        <div class="form-group{% if form.username.errors %} has-error{% endif %}">
          <label for="username">{% trans 'Username' %}</label>
          <input type="text" class="form-control" id="username" name="username">
          {% for error in form.username.errors %}
            <label class="control-label">{{ error }}</label>
          {% endfor %}
        </div>
        <div class="form-group{% if form.password.errors %} has-error{% endif %}">
          <label for="password">{% trans 'Password' %}</label>
          <input type="password" class="form-control" id="password" name="password">
          {% for error in form.password.errors %}
            <label class="control-label">{{ error }}</label>
          {% endfor %}
        </div>
        <div class="form-group">
          <button type="submit" class="btn btn-default">{% trans 'Log in' %}</button>
          <a href="{% url 'signup' %}" class="btn btn-link">{% trans 'Sign up for muHUB' %}</a>
        </div>
      </form>
    </div>
  </div>
{% endblock body %}

编辑2

添加追溯

Traceback:

File "C:\Anaconda2\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Anaconda2\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Anaconda2\lib\site-packages\django\contrib\auth\views.py" in inner
  49.         return func(*args, **kwargs)

File "C:\Anaconda2\lib\site-packages\django\views\decorators\debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "C:\Anaconda2\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Anaconda2\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "C:\Anaconda2\lib\site-packages\django\contrib\auth\views.py" in login
  73.                 redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

File "C:\Anaconda2\lib\site-packages\django\shortcuts.py" in resolve_url
  204.         return urlresolvers.reverse(to, args=args, kwargs=kwargs)

File "C:\Anaconda2\lib\site-packages\django\core\urlresolvers.py" in reverse
  600.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "C:\Anaconda2\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  508.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /login/
Exception Value: Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 个答案:

答案 0 :(得分:1)

错误发生在您的LOGIN_REDIRECT_URL中,这是堆栈跟踪中与您自己的代码连接的唯一代码。

File "C:\Anaconda2\lib\site-packages\django\contrib\auth\views.py" in login
  73.                 redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

我怀疑您的登录重定向网址是在反向帮助下构建的,或者是否设置不正确。