【问题标题】:NoReverseMatch at /Gyobera//Gyobera/ 的 NoReverseMatch
【发布时间】:2016-02-14 18:52:23
【问题描述】:

我是 Django 新手;虽然这里已经解决了相同的错误,但老实说,我无法找到我的代码出错的地方。

这是我得到的错误:

NoReverseMatch at /Gyobera/
Reverse for 'classification' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/Gyobera/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'classification' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$']
Exception Location: C:\Python34\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.2
Python Path:    
['C:\\Users\\Scott Businge\\Ewange',
 'C:\\Python34\\lib\\site-packages\\pip-7.1.0-py3.4.egg',
 'C:\\Users\\Scott Businge\\Ewange',
 'C:\\WINDOWS\\SYSTEM32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Users\\Scott Businge\\AppData\\Roaming\\Python\\Python34\\site-packages',
 'C:\\Python34\\lib\\site-packages']
Server time:    Mon, 15 Feb 2016 08:48:43 +0300

链接无法点击显示详细页面,每次点击都会显示索引页面的错误,我将在此处发布。我只使用项目 urls(Ewange)

index.html

        <h2><strong>Main Classifications</strong></h2>
          {% if classifications %}
            <ul>
                {% for classification in classifications %}
                 <!-- Following line changed to add an HTML hyperlink -->
                <li><a href="{% url 'classification' classification.url %}">{{ classification.name }}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <strong>There are no classifications present.</strong>
        {% endif %}
{% endblock %}

urls.py

url(r'^classification/(?P<classification_name_url>\w+)/$', views.classification,
                           name='classification'),

views.py

def index(request):
    context = RequestContext(request)
    classification_list = Classification.objects.order_by('-likes')[:6]
    context_dict = {'classifications': classification_list}
    for classification in classification_list:
        classification.url = classification.name.replace(' ', '_')
    return render_to_response('index.html', context_dict, context)

def classification(request, classification_name_url):
    context = RequestContext(request)
    classification_name = classification_name_url.replace('_', ' ')
    context_dict = {'classification_name': classification_name, 'classification_name_url': classification_name_url}
    try:
        classification = Classification.objects.get(name=classification_name)
        context_dict['classification'] = classification
        lists = List.objects.filter(classification=classification)
        context_dict['lists'] = lists
    except Classification.DoesNotExist:
        return render_to_response('gyobera/classification.html', context_dict, context)

分类.html

<h1>{{ classification_name }}</h1>
{% if classification %}
    {% if lists %}
    <ul>
        {% for list in lists %}
        <li><a href="{{ list.title }}">{{ list.title }}</a></li>
        {% endfor %}
    </ul>
    {% else %}
        <strong>No lists currently in classification.</strong>
    {% endif %}
{% else %}
    The specified classification {{ classification_name }} does not exist!
{% endif %}

settings.py

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

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'Ewange.urls'

当我运行服务器时,我仍然得到“NoReverseMatchError” 这是 index.html 中 urls.py、views.py 和链接的当前状态。 我要问的是,在更正了classification.list_set之后,你是否必须分别更改views和urls.py中的context_dict和url匹配..

NoReverseMatch at /Gyobera/
Reverse for 'classification' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/Gyobera/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'classification' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['classification/(?P<classification_name_url>\\w+)/$']
Exception Location: C:\Python34\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496

urls.py

url(r'^classification/(?P<classification_name_url>\w+)/$', views.classification,
                           name='classification'),

views.py

def classification(request, classification_name_url):
    context = RequestContext(request)
    classification_name = classification_name_url.replace('_', ' ')
    context_dict = {'classification_name': classification_name, 'classification_name_url': classification_name_url}
    try:
        classification = Classification.objects.get(name=classification_name)
        context_dict['classification'] = classification
        lists = List.objects.filter(classification=classification)
        context_dict['lists'] = lists
    except Classification.DoesNotExist:
        return render_to_response('gyobera/classification.html', context_dict, context)

index.html

  {% if classifications %}
    <ul>
        {% for classification in classifications %}
         <!-- Following line changed to add an HTML hyperlink -->
        <li><a href="{% url 'classification' classification.list_set.url %}"></a></li>
        {% endfor %}
    </ul>
{% else %}

【问题讨论】:

  • 请发布堆栈跟踪。
  • 让我把它贴下来@Sayse

标签: python django


【解决方案1】:

您收到错误是因为您正在查看的分类没有 url,并且它没有 url,因为您从未在视图中保存对模型的更改。但是,我不相信每次有人加载索引页面时保存对模型的更改是个好主意。

def index(request):
    context = RequestContext(request)
    classification_list = Classification.objects.order_by('-likes')[:6]
    for classification in classification_list:
        classification.url = classification.name.replace(' ', '_')
        classification.save()

    context_dict = {'classifications': classification_list}
    return render_to_response('index.html', context_dict, context)

【讨论】:

    【解决方案2】:
    <li><a href="{% url 'classification' classification.url %}">
    ...
    

    分类没有 url 字段,因此“分类”url 没有参数。这就是错误显示with arguments '()' and keyword arguments '{} 的原因,即:没有参数。

    实际关系:一个分类有很多列表,每个列表都有一个url字段。所以一个分类有多个 url,通过 List 模型。

    要访问单个分类的所有列表,请使用:

    classification.list_set #list_set is the default, when you don't use related_name
    

    这可以给出多个列表,因此要么循环遍历此分类的列表,要么选择一个,例如:

    <li><a href="{% url 'classification' classification.list_set.first().url %}">
    

    【讨论】:

    • 它显示的是相同的@Aviah。让我发布堆栈跟踪
    • 让我们尝试调试它,我怀疑它与名称/url有关。而不是{% url 'classification' classification.url %},请尝试表中的值,例如{% url 'classification' foobaz %}。另外,能发一下模型吗?
    • 是的,模型在这里。
    • 即使我从表中输入一个值,它也会显示“NoRreverseMatchError”@Aviah
    • 感谢 Aviah,但我是否必须反映 urls.py 和 views.py 中的更改,即把“classification.list_set”放在我有“classification_name_url”的地方,因为当我放那个时,它显示第一个列表中没有参数.. 在 IDE 中,它在 first() 时显示错误 = 预期 %
    猜你喜欢
    • 2018-02-03
    • 2017-03-26
    • 2014-02-10
    • 2017-05-21
    • 1970-01-01
    • 2018-12-09
    • 2020-10-28
    相关资源
    最近更新 更多