Django 1.6:未定义名称“sitemaps” [英] Django 1.6: name 'sitemaps' is not defined

查看:204
本文介绍了Django 1.6:未定义名称“sitemaps”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的django应用程序中实现站点地图,但是我收到以下错误。我正在使用django sitemap的框架。我不知道我做错了什么

I'm trying to implement sitemaps in my django application but i get the following error. I'm using the django sitemap's framework. I don't know what I'm doing wrong.

Traceback:
File "mysite/urls.py" in <module>
  3. from sitemap import *
File "mysite/sitemap.py" in <module>
  5. class Sitemap(sitemaps.Sitemap):

Exception Type: NameError at /
Exception Value: name 'sitemaps' is not defined

这是sitemap.py文件

Here is the sitemap.py file

from django.contrib.sitemaps import Sitemap
from meddy1.models import Doctor
import datetime

class Sitemap(Sitemap):
    def __init__(self, names):
        self.names = names

    def items(self):
        return self.names

    def changefreq(self, obj):
        return 'weekly'

    def lastmod(self, obj):
        return datetime.datetime.now()

    def location(self, obj):
        return reverse(obj)


class DoctorSitemap(Sitemap):
    changefreq = "Daily"
    priority = 1

    def items(self):
        return Doctor.objects.all()

    def lastmod(self, obj):
        return obj.date

以下是来自django.conf.urls导入模式的urls.py文件

Here is the urls.py file

from django.conf.urls import patterns, include, url
from django.contrib import admin
from sitemap import *

admin.autodiscover()

sitemaps = {
    'pages':Sitemap(['homepage_imprint', 'homepage_archive']),
    'doctor':DoctorSitemap,
    'site':Sitemap(['name_of_url', 'name_of_url']),
}

urlpatterns = patterns('',
    url(r'^', include('meddy1.urls')),
    url(r'^', include('django.contrib.auth.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^sitemap\.xml', include('django.contrib.sitemaps.views.sitemap'),{'sitemaps': sitemaps}), 
)


推荐答案

您从模块导入 Sitemaps ,而不是模块本身。删除模块名称:

You imported Sitemaps from the module, not the module itself. Remove the module name:

class Sitemap(Sitemap):

这将只是关于工作,即使您正在替换导入的类。

This will just about work, even though you are replacing the imported class here.

另外也可以说清楚你在做什么,调整你的模块导入。更改导入从

Alternatively and arguably clearer as to what you are doing, adjust your import of the module. Change the import from:

from django.contrib.sitemaps import Sitemap

to:

from django.contrib import sitemaps

这篇关于Django 1.6:未定义名称“sitemaps”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆