0
我有一個問題與我的模型設置站點地圖。我收到以下錯誤。未找到參數'()'和關鍵字參數'{}'的'5555155555'反向。嘗試了0個模式:[]。我想我必須通過一個slu to的位置,但不是100%確定我將如何完成這項任務。Django站點地圖和絕對URL
#sitemap.py
from django.contrib.sitemaps import Sitemap
from django.contrib.flatpages.models import FlatPage
from django.urls import reverse
from phonenumbers.models import Phone
class PhoneSitemap(Sitemap):
changefreq = "always"
priority = 0.5
def items(self):
return Phone.objects.all()
def location(self, obj):
return reverse(obj)
def lastmode(self, obj):
return obj.created
class FlatpageSitemap(Sitemap):
changefreq = "always"
priority = 0.5
def items(self):
return FlatPage.objects.all()
#models.py
class Phone(models.Model):
phone_number = models.CharField(blank=False, max_length=15) # validators should be a list
name = models.CharField(max_length=50)
message = models.TextField()
caller = models.CharField(max_length=100, choices=CALLER_CHOICES, default="Unknown")
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=250, unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.phone_number)
super(Phone, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('phonenumbers:phone_details', args=[self.slug])
def __str__(self):
return self.phone_number
#urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.flatpages import views
from django.contrib.sitemaps.views import sitemap
from .sitemap import PhoneSitemap, FlatpageSitemap
from info.views import home
sitemaps = {
'posts': PhoneSitemap,
'pages': FlatpageSitemap,
}
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', home, name="home"),
url(r'^privacy-policy/$', views.flatpage, {'url': '/privacy-policy/'}, name='privacy-policy'),
url(r'^terms-and-conditions/$', views.flatpage, {'url': '/terms-and-conditions/'}, name='terms-and-conditions'),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
url(r'^whocalledme/', include('phonenumbers.urls', app_name="phonenumbers", namespace="phonenumbers")),
]
phonenumbers.urls
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^search/$', views.search_numbers, name="search_numbers"),
url(r'^number/(?P<slug>[-\w]+)/$', views.phone_detail, name="phone_detail"),
url(r'^add_number/$', views.add_number, name='add_number'),
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/sitemap.xml
Django Version: 1.10.4
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'whitenoise.runserver_nostatic',
'crispy_forms',
'django.contrib.sitemaps',
'phonenumbers']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/contrib/sitemaps/views.py" in inner
16. response = func(request, *args, **kwargs)
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/contrib/sitemaps/views.py" in sitemap
68. protocol=req_protocol))
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/contrib/sitemaps/__init__.py" in get_urls
105. urls = self._urls(page, protocol, domain)
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/contrib/sitemaps/__init__.py" in _urls
114. loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/contrib/sitemaps/__init__.py" in __get
62. return attr(obj)
File "/Users/Tommy/Desktop/django/info/info/info/sitemap.py" in location
15. return obj.get_absolute_url()
File "/Users/Tommy/Desktop/django/info/info/phonenumbers/models.py" in get_absolute_url
25. return reverse('phonenumbers:phone_details', kwargs={'slug': self.slug})
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/urls/base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/Tommy/Desktop/django/info/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /sitemap.xml
Exception Value: Reverse for 'phone_details' with arguments '()' and keyword arguments '{'slug': '2159642387'}' not found. 0 pattern(s) tried: []
感謝您的回覆。我仍然得到「get_absolute_url()需要1個位置參數,但是2個被賦予了」當我改變它。 –
請把你的問題的完整回溯。這應該會有所幫助。 – grantmcconnaughey
在代碼底部添加 –