Django Site_ID和Sitemaps

时间:2016-02-20 00:59:05

标签: django

我正在尝试为Google Search Console设置站点地图,而且在设置Django“站点”框架方面遇到了问题。当我在项目设置文件中的settings.py中放入“SITE_ID = 1”时,运行时会收到警告

python manage.py ping_google [/sitemap.xml]

因为它返回“urllib2.HTTPError:HTTP错误400:错误请求”,并且不生成sitemap.xml。

# sitemaps.py
from django.contrib import sitemaps
from django.core.urlresolvers import reverse

class StaticViewSitemap(sitemaps.Sitemap):
    priority = 0.5
    changefreq = 'daily'

    def items(self):
        return ['index']

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

urls.py

from . import views
from .sitemaps import StaticViewSitemap

sitemaps = {
    'static': StaticViewSitemap,
}

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},
        name='django.contrib.sitemaps.views.sitemap'),
]

我在INSTALLED_APPS下添加了django.contrib.sitemapsdjango.contrib.sites。我只有一个网站,或“应用程序”。我在逻辑方面遗漏了一些东西,不知道如何正确定义SITE_ID。如果我在项目settings.py

中删除了SITE_ID = 1,则会收到错误“no site_id defined”

3 个答案:

答案 0 :(得分:1)

您可以尝试使用更容易理解的请求:

PING_URL = "http://www.google.com/webmasters/tools/ping"

def ping_google(sitemap_url=None, ping_url=PING_URL):
    if not sitemap_url:
        raise Exception('sitemap url must be given')

    current_site = Site.objects.get_current()
    url = "http://%s%s" % (current_site.domain, sitemap_url)
    requests.get(
        ping_url, params={'sitemap': url}   
    )

原始ping_google功能位于此处:

...\site-packages\django\contrib\sitemaps\__init__.py

答案 1 :(得分:1)

我忘了删除public object localSettingSoundState; public object localSoundVolumeSetting; ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; public void PlaySound(MediaElement mediaElementName) { localSettingSoundState = localSettings.Values["localSoundSetting"]; MediaElementVolume = Convert.ToDouble(localSettings.Values["localSoundVolumeSetting"]); if (localSettingSoundState.ToString() != "false") { mediaElementName.Play(); } } 中的方括号,导致命令不起作用。

答案 2 :(得分:0)

如果你想直接从视图运行命令:

from django.core.management import call_command

def daily_crons_view(request):
    # Ping Google
    return call_command('ping_google [/sitemap.xml]')