我正在尝试让我的网站拥有一个包含站点地图的网页,以便Google可以更轻松地为我的网站建立索引。我得到的错误是:“找不到'detail_view'的反向。'detail_view'不是有效的视图函数或模式名称”。我已经在多个站点地图上取得了成功,但我无法使用其中一个来起作用。有人可以在这里看到我的问题吗?我相信这是相关代码?我将不胜感激任何帮助!预先谢谢你!
models.py:
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def get_absolute_url(self):
return reverse('detail', kwargs={'question_id':self.id })
urls.py:#在我的项目中
from django.conf.urls import include, url
from django.urls import path
from django.contrib.sitemaps.views import sitemap
from django.contrib import admin
from django.contrib.sitemaps import GenericSitemap
admin.autodiscover()
import hello.views
from polls.models import Question
info_dict3 = {
'queryset': Question.objects.all(),
}
sitemaps3 = {'polls': GenericSitemap(info_dict3, priority=0.6)}
urlpatterns = [
path('sitemap3.xml', sitemap, {'sitemaps': sitemaps3}, ...
name='django.contrib.sitemaps.views.sitemaps3'),
]
views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect
from .models import Question
from django.urls import reverse
from django.views import generic
def detail_view(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
urls.py:#在我的应用中
from django.urls import path
from django.contrib.sitemaps.views import sitemap
from django.contrib import admin
from django.contrib.sitemaps import GenericSitemap
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail_view, name='detail_view'),
]