Django NoReverseMatch返回render()

时间:2017-07-10 15:02:55

标签: python django django-urls

我总是收到NoReverseMatch错误,我不知道为什么。 这是错误消息:

   / suche / any-thing /

中的NoReverseMatch      

反向'article_search',参数'(u'any thing',)'找不到。尝试过1种模式:['suche /(?P [ - \ w] +)/ $']

正如你所看到的,我正在输入一个带有“ - ”而不是空格的网址,但是Django正在寻找带有空格而不是“ - ”的网址模式。

这是我的网址格式:

    url(r'suche/(?P<search>[-\w]+)/$', views.article_search_view, name='article_search'),

令人惊讶的是Django开始计算我的article_search_view,看起来像这样:

def article_search_view(request, search=None):
"""Filters all articles depending on the search and renders them"""

articles = get_active_not_rented_articles()
search = re.sub(r"[-]", ' ', search)
articles = articles.filter(title__icontains=search)
articles = aply_sort(request, articles)
orderd_by = articles[0].get('filter')
articles = articles[1]
return render(request, 'article/list.html', {'object_list':articles, 'url_origin':'article_search', 'parameter':search,
'orderd_by':orderd_by})

当我检查“print()”语句时,在调用return render(...)语句时会引发错误。 如果我改为返回重定向(...),则不会引发错误。 为了完整起见,我的文章/ list.html模板:

{% extends "base.html" %}
{% load static %}

{% block content %}

<div id =articles>
<div class="info_filter">
<div class="header_info_filter">
  {% if orderd_by == "not" %}
  <h1>Neueste Artikel</h1>
  {% endif %}
  {% if orderd_by == "distance" %}
  <h1>Artikel in Ihrer Nähe</h1>
  {% endif %}
  {% if orderd_by == "price_asc" %}
  <h1>Günstigste Artikel zuerst</h1>
  {% endif %}
  {% if orderd_by == "price_des" %}
  <h1>Teuerste Artikel zuerst</h1>
  {% endif %}
</div>

<div class="selection">
  {% if parameter1 %}
  <form action="{% url url_origin parameter1 parameter2 %}" method="post" accept-charset="utf-8">
    {% else %}
    {% if parameter %}
    <form action="{% url url_origin parameter %}" method="post" accept-charset="utf-8">
      {% else %}
      <form action="{% url url_origin %}" method="post" accept-charset="utf-8">
        {% endif %}
        {% endif %}
        {% csrf_token %}
        <div class="select_filter">
          <select name="filter" id="filter" >
            <option value="distance">Entfernung</option>
            <option value="price_asc">Preis, aufsteigend</option>
            <option value="price_des">Preis, absteigend</option>
          </select>
          <div class="search_filter_btn">
            <button type="submit" name="button">Sortieren</button>
          </div>
        </div>

      </form>
    </div>
    {% if parent_categorys %}
    <div class="category-path">
      Ergebnisse für:
      {% for category in parent_categorys %}
      <a href="{% url 'article_category' category.slug %}"> > {{ category.name }}</a>
      {% endfor %}
      {% if parameter2 %}
      : {{ parameter2}}
      {% endif %}
    </div>
    {% else %}
    Ergebnisse für: {{ parameter}}
    {% endif %}
  </div>
  <div id="main" class="article_list">
    {% for article in object_list %}
    <div class="item">
      <div class="list_img">
        <a href="{{ article.get_absolute_url }}">
          <img src="{% if article.main_picture %}{{ article.main_picture.url }}{% else %}{% static "img/no_image.png" %}{% endif %}">
        </a>

      </div>
      <div class= "articles_fee" >
        {{ article.fee }} €
      </div>
      <a href="{{ article.get_absolute_url }}">{{ article.title }}</a>
    </div>
    {% endfor %}
  </div>

</div>
{% endblock %}

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

In your view, you change search and replace hyphens with spaces. This causes an error when you use the url tag in your template, because the URL pattern does not allow spaces.

{% url url_origin parameter %}

You could fix the problem by adding the original search slug to the template context:

def article_search_view(request, search=None):
    """Filters all articles depending on the search and renders them"""
    search_slug = search
    articles = get_active_not_rented_articles()
    search = re.sub(r"[-]", ' ', search)
    ...
    return render(request, 'article/list.html', {'object_list':articles, 'url_origin':'article_search', 'parameter':search, 'search_slug': search_slug, 'orderd_by':orderd_by})

Then change the url tag:

{% url url_origin search_slug %}