Django NameError urls.py

2019-06-04 03:37发布

问题:

Im getting a name error: name sitemaps is not defined from my urls.py when I try to integrate sitemaps with my application.

From my urls.py:

from django.contrib.sitemaps import Sitemap

(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),

Is there something wrong with this regular expression from my urls.py? Or is there another problem going on?

Thanks for your input

回答1:

Generally you will do something like this -

from django.contrib.sitemaps import Sitemap, FlatPageSitemap

sitemaps = {
  'site': Sitemap,
  'flatpages': FlatPageSitemap,
}

# ..
# Some url patterns. urlpatterns must be defined by now
# ..

urlpatterns += patterns("",
  url(r'^sitemap\.xml$', 
      'django.contrib.sitemaps.views.sitemap', 
      {'sitemaps': sitemaps}
  ),
)


回答2:

From the docs:

sitemaps should be a dictionary that maps a short section label (e.g., blog or news) to its Sitemap class (e.g., BlogSitemap or NewsSitemap). It may also map to an instance of a Sitemap class (e.g., BlogSitemap(some_var)).

So... define it.