[英]Error during template rendering (NoReverseMatch) in Django
I am trying to submit the form to my view:我正在尝试将表单提交给我的视图:
in trending.html:在趋势中。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 %}
in views.py:在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})
in urls.py:在 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'),
]
I got the following error:我收到以下错误:
NoReverseMatch at /trending/ Reverse for 'trending_snippets' with arguments '('c-sharp',)' not found. NoReverseMatch at /trending/ Reverse 为 'trending_snippets' 与 arguments '('c-sharp',)' 未找到。 2 pattern(s) tried: ['trending/(?P[\w]+)/$', 'trending/$']
尝试了 2 种模式:['trending/(?P[\w]+)/$', 'trending/$']
Exception Type: NoReverseMatch异常类型:NoReverseMatch
Error during template rendering模板渲染期间出错
Reference website: overiq.com参考网址: overiq.com
Reference website Link: https://overiq.com/django-1-11/creating-trending-snippet-page/参考网站链接: https://overiq.com/django-1-11/creating-trending-snippet-page/
Python version: 3.8.2 Python 版本: 3.8.2
Django version: 3.0.5 Django 版本: 3.0.5
OS: Windows 8.1(32 bit)操作系统: Windows 8.1(32位)
To match c-sharp
, which contains a hyphen, you need to change [\w]
to [-\w]
.要匹配包含连字符的
c-sharp
,您需要将[\w]
更改为[-\w]
。
url('^trending/(?P<language_slug>[-\w]+)/$', views.trending_snippets, name='trending_snippets'),
Since it's a slug field you're matching, you can use the built-in slug path converter Django provides to match it by using path
instead of url
.由于它是您要匹配的 slug 字段,因此您可以使用Django 提供的内置 slug 路径转换器通过使用
path
而不是url
来匹配它。
Change:改变:
url('^trending/(?P<language_slug>[\w]+)/$', views.trending_snippets, name='trending_snippets'),
to:至:
path('trending/<slug:language_slug>)/', views.trending_snippets, name='trending_snippets'),
Note that slug:
matches hyphens, underscores, and also ASCII letters and numbers.请注意
slug:
匹配连字符、下划线以及 ASCII 字母和数字。
url()
is just an alias to re_path()
and may be deprecated in the future , so you should change your code accordingly. url()
只是re_path()
的别名,将来可能会被弃用,因此您应该相应地更改代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.