使用参数'('',)'找不到“详细信息”的反转。尝试了1种模式:['详情/(?P [。\ - \ w] +)$']
打开主页时出现此错误。所有其他页面似乎都有效。
urls.py
app_name = 'main'
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'ask-question/$', views.question, name='ask-question'),
url(r'^details/(?P<slug>[.\-\w]+)$', views.details, name='details'),
]
主页的views.py
def home(request):
questions = Question.objects.all().order_by("-date")
numbers = Question.objects.all().count()
numbers2 = Answer.objects.all().count()
total_users = User.objects.all().count()
# counting answers on specific questions
results = Question.objects.annotate(num_answers=Count('answer')).order_by("-date")
# PAGINATION ===============================
page = request.GET.get('page', 1)
paginator = Paginator(results,10)
try:
results = paginator.page(page)
except PageNotAnInteger:
results = paginator.page(1)
except EmptyPage:
results = paginator.page(paginator.num_pages)
# end of counting
empty = []
for a in Answer.objects.all():
idd = a.id
question_id = (a.question_id)
empty.append(str(question_id))
repeatition = Counter(empty)
# i = 0
# trend_list = []
# for x in range(len(repeatition)):
# new = repeatition.most_common()[i][0]
# trend_list.append(new)
# i += 1
# if len(trend_list) != 0:
# trend = Question.objects.get(id=trend_list[0])
# else:
# trend = 'No Trending Category'
# getting the answers to all questions in the front page
# search the questions ============
query= request.GET.get("q")
if query:
short_list = Question.objects.all()
questions = short_list.filter(title__icontains=query)
resulted = questions.annotate(num_answers=Count('answer'))
counted = questions.count()
context1 = {
'questions': questions,
'query': query,
'counted': counted,
'resulted': resulted,
}
return render(request, 'main/search.html',context1)
context = {
'questions': questions,
'numbers': numbers,
'numbers2': numbers2,
'total_users': total_users,
# 'trend': trend,
'results': results,
}
return render(request, 'main/index.html', context)
的index.html
{% extends 'main/base.html' %} {% block content %}
<div class="container">
{% for message in messages %}
<div class="alert alert-{{ message.tags }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button> {{ message }}
</div>
{% endfor %}
</div>
<!-- end of the message container -->
<!-- start of the actual questions -->
<div class="container-fluid">
<h1 class="text-left" id="brand"> Recently Added Questions</h1>
<div class="row">
<div class="col-md-8">
<div id="qtns">
{% for q in results %}
<a href="{% url 'main:details' q.slug %}" id="questions">
<h4 id="titleq">{{ q.title }}</h4>
</a>
<a href="{% url 'main:filter' q.category %}">
<p class="badge badge-info">{{ q.category }}</p>
</a>
<p id="titleq">{{ q.date |timesince }} ago. <a href="{% url 'accounts:profile' q.user %}" id="questions" style=" padding: 2px; border:1px solid orange;">{{ q.user }}</a></p>
<div class="text-right">
<p>Answers: {{q.num_answers}}</p>
</div>
<hr> {% endfor %}
</div>
</div>
<div class="col-md-3">
<div id="qtns1">
<br> {% if not user.is_authenticated %}
<a href="{% url 'accounts:login' %}?next={% url 'main:ask-question' %}" class="btn btn-md btn-info">Ask Question</a><br><br> {% else %}
<a href="{% url 'main:ask-question' %}" class="btn btn-md btn-warning">Ask Quesiton</a><br><br> {% endif %}
<div class="qtns2">
<h1 id="titleq">Statistics</h1>
<p>Questions asked: {{numbers}} </p>
<p>Answers: {{ numbers2 }}</p>
<p>Total Users: {{ total_users }}</p>
</div> <br>
<div class="qtns2">
Most answered:
<a href="{% url 'main:details' trend.slug %}" style="text-decoration:none;">
<p>{{ trend }}</a> - <i>{{ trend.user }}</i></p>
<p><i>Posted On: </i>{{ trend.date}}</p>
</div>
<br>
<div class="qtns2">
Most Questions By:
<a href="{% url 'accounts:profile' trend.user %}" style="text-decoration:none;">
<p>{{ trend.user }}</p>
</a>
</div>
</div>
</div>
</div>
<br>
<!-- pagination for the limited display of questions -->
<nav aria-label='pagination'>
{% if results.has_other_pages %}
<ul class="pagination">
{% if results.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ results.previous_page_number }}">«</a></li>
{% else %}
<li class="page-item disabled"><span class="page-link">«</span></li>
{% endif %} {% for i in results.paginator.page_range %} {% if results.number == i %}
<li class=" page-item active"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %} {% endfor %} {% if questions.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ results.next_page_number }}">»</a></li>
{% else %}
<li class=" page-item disabled"><span class="page-link">»</span></li>
{% endif %}
</ul>
</nav>
</div>
{% endif %} {% endblock %}
所有其他页面都正常工作,但主页返回“详细信息”.....未找到。什么似乎是问题,我无法弄清楚。任何人都可以看看我的代码并告诉我我的错误。提前谢谢。
答案 0 :(得分:1)
每次遇到错误都需要调试策略。在NoReverseMatch案例中,策略是:
通过问自己三个问题来检查案例是否有效:
所有答案都必须是肯定的。如果答案是否定的,请修复它。
调用'详细信息'的反转的情况是:
<a href="{% url 'main:details' q.slug %}" id="questions">
<a href="{% url 'main:details' trend.slug %}" style="text-decoration:none;">
上下文中有q
和trend
吗? q
是results
对象的一部分,这是肯定的,trend
不在上下文中,因为您在视图中注释了部分代码。 trend.slug
因此返回 nothing ,并且没有找不到“详细信息”,因为 slug 是预期的。
如果你解决了这个问题,这个错误就会消失。