【问题标题】:Error during template rendering (NoReverseMatch) in Django在 Django 中的模板渲染(NoReverseMatch)期间出错
【发布时间】:2020-08-04 09:38:24
【问题描述】:

我正在尝试将表单提交给我的视图:

在trending.html中:

{% extends 'djangobin/base.html' %}

{% load static %}
{% load humanize %}

{% block title %}
    Trending {{ lang.name }} Snippets - {{ block.super }}
{% endblock %}

{% block main %}

    <h5><i class="fas fa-chart-line"></i> Trending {{ lang.name }} Snippets</h5>
    <hr>

    <table class="table">
        <thead>
        <tr>
            <th>Title</th>
            <th>Date</th>
            <th>Hits</th>
            <th>Language</th>
            <th>User</th>
        </tr>
        </thead>
        <tbody>

        {% for snippet in snippets %}
            <tr>
                <td><i class="fas fa-globe"></i>
                    <a href="{{ snippet.get_absolute_url }}">{{ snippet.title }}</a>
                </td>
                <td title="{{ snippet.created_on }}">{{ snippet.created_on|naturaltime }}</td>
                <td>{{ snippet.hits }}</td>
                <td><a href="{% url 'trending_snippets' snippet.language.slug  %}">{{ snippet.language }}</a></td>
                {% if not snippet.user.profile.private %}
                    <td><a href="{{ snippet.user.profile.get_absolute_url }}">{{ snippet.user.username|title }}</a></td>
                {% else %}
                    <td>-</td>
                {% endif %}

            </tr>
        {% empty %}
            <tr class="text-center">
                <td colspan="4">There are no snippets.</td>
            </tr>
        {% endfor %}

        </tbody>
    </table>

{% endblock %}

在views.py中:

from django.shortcuts import HttpResponse, render, redirect, get_object_or_404, reverse
from .forms import SnippetForm
from .models import Language, Snippet

def trending_snippets(request, language_slug=''):
    lang = None
    snippets = Snippet.objects
    if language_slug:
        snippets = snippets.filter(language__slug=language_slug)
        lang = get_object_or_404(Language, slug=language_slug)
    snippets = snippets.all()
    return render(request, 'djangobin/trending.html', {'snippets': snippets, 'lang': lang})

在 urls.py 中:

from django.conf.urls import url
from . import views as views
urlpatterns = [
    url('^trending/$', views.trending_snippets, name='trending_snippets'),
    url('^trending/(?P<language_slug>[\w]+)/$', views.trending_snippets, name='trending_snippets'),
]

我收到以下错误:

NoReverseMatch 在 /trending/ 未找到带有参数 '('c-sharp',)' 的 'trending_sn-ps' 的反向。尝试了 2 种模式:['trending/(?P[\w]+)/$', 'trending/$']

异常类型:NoReverseMatch

模板渲染时出错

参考网站: overiq.com

参考网站链接:https://overiq.com/django-1-11/creating-trending-snippet-page/

Python 版本: 3.8.2

Django 版本: 3.0.5

操作系统: Windows 8.1(32 位)

【问题讨论】:

  • 你运行的是哪个版本的 Django?
  • Django 版本 - 3.0.5

标签: python django django-forms django-views django-templates


【解决方案1】:

由于您要匹配的是 slug 字段,因此您可以使用 built-in slug path converter Django provides 来匹配它,方法是使用 path 而不是 url

变化:

url('^trending/(?P<language_slug>[\w]+)/$', views.trending_snippets, name='trending_snippets'),

到:

path('trending/<slug:language_slug>)/', views.trending_snippets, name='trending_snippets'),

请注意,slug:匹配连字符、下划线以及 ASCII 字母和数字。

url() 只是re_path()may be deprecated in the future 的别名,因此您应该相应地更改您的代码。

【讨论】:

    【解决方案2】:

    要匹配包含连字符的c-sharp,您需要将[\w] 更改为[-\w]

    url('^trending/(?P<language_slug>[-\w]+)/$', views.trending_snippets, name='trending_snippets'),
    

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 2020-11-18
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2017-01-31
      相关资源
      最近更新 更多