我正在用django编写一个应用程序。一切都工作正常,我一直在应用程序的另一页工作,然后我决定回到主页,我得到了这个错误。
Reverse for 'more' with keyword arguments '{u'slug': u''}' not found. 1 pattern(s) tried: ['(?P<slug>[-\\w]+)/$']
一切都很完美,我无法理解为什么。 如果我将模板更改为另一个模板让我们说hello.html整个网址响应得很好但是一旦我将其更改回index.html我就会得到同样的错误。任何帮助将不胜感激
form.py
from django import forms
from posts.models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = [
"title",
"content",
"image",
"draft",
"publish",
]
views.py
def index(request):
results = Post.objects.all().filter(draft=False).filter(publish__lte=timezone.now())
que = request.GET.get("q")
if que:
results =results.filter(
Q(title__icontains=que)|
Q(content__icontains=que)).distinct()
paginator = Paginator(results, 8) # Show 25 contacts per page
pages ="page"
page = request.GET.get('page')
try:
query = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
query = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
query = paginator.page(paginator.num_pages)
context = {
"objects": query,
"pages": pages
}
template = 'index2.html'
return render(request,template,context)
HTML
{% extends 'base.html' %}
{% load staticfiles %}
{% block content %}
<h1>This is Home</h1>
{% include 'messages.html'%}
<form method="GET" action="">
{% csrf_token %}
<input type="text" name="q" value="{{ request.GET.q }}" placeholder="search" >
<input type="submit" name="submit" value="Go!">
</form>
{% for obj in objects %}
<h1> <a href="{{ obj.get_absolute_url }}">{{ obj.title }}</a></h1><br>
{% if obj.image %}
<img src="{{ obj.image.url }}" alt="{{ obj.title }}" width="250" height="250"><br>
{% endif %}
{{ obj.content|safe|truncatewords:20 }}
{{ obj.timestamp }}<br>
{{ obj.updated }}<br>
{% endfor %}
<br>
<div class="pagination">
<span class="step-links">
{% if contacts.has_previous %}
<a href="?{{ pages }}={{ objects.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">previous</a>
{% endif %}
<span class="current">
Page {{ objects.number }} of {{ objects.paginator.num_pages }}.
</span>
{% if objects.has_next %}
<a href="?{{ pages }}={{ objects.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}">next</a>
{% endif %}
</span>
</div>
{% endblock %}
模型中的绝对网址
def get_absolute_url(self):
return reverse("more", kwargs={"slug": self.slug})
网址格式
url(r'^$', posts_views.index, name='index'),