我想根据我的服务器是生产服务器还是开发服务器来使用不同的 robots.txt
文件。
为此,我想在 urls.py
中以不同的方式路由请求:
urlpatterns = patterns('',
// usual patterns here
)
if settings.IS_PRODUCTION:
urlpatterns.append((r'^robots\.txt$', direct_to_template, {'template': 'robots_production.txt', 'mimetype': 'text/plain'}))
else:
urlpatterns.append((r'^robots\.txt$', direct_to_template, {'template': 'robots_dev.txt', 'mimetype': 'text/plain'}))
但是,这不起作用,因为我没有正确使用 patterns
对象:我在/robots.txt 处收到 AttributeError - 'tuple' object has no attribute 'resolve '
。
如何在 Django 中正确执行此操作?
最佳答案
试试这个:
if settings.IS_PRODUCTION:
additional_settings = patterns('',
(r'^robots\.txt$', direct_to_template, {'template': 'robots_production.txt', 'mimetype': 'text/plain'}),
)
else:
additional_settings = patterns('',
(r'^robots\.txt$', direct_to_template, {'template': 'robots_dev.txt', 'mimetype': 'text/plain'}),
)
urlpatterns += additional_settings
由于您要附加 tuple
类型,因此 append
不起作用。
另外,pattern()
会为您调用urlresolver
。就您而言,您不是,因此出现错误。
关于Django:条件 URL 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17111477/