【问题标题】:Using StaticGenerator to generate static HTML from a Django project使用 StaticGenerator 从 Django 项目生成静态 HTML
【发布时间】:2023-03-12 14:24:01
【问题描述】:

我正在尝试为我在 Django 中构建的网站生成静态 HTML。到目前为止我想出的是StaticGenerator。在我看来它应该类似于 github 页面中的示例,但我已经编写了下面的代码,它给出了下面的错误。

代码:

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
os.environ["DJANGO_SETTINGS_MODULE"] = "site.settings"
quick_publish('/')

错误(为简洁而编辑):

Traceback (most recent call last):
    quick_publish('/')
  File "/usr/local/lib/python2.6/dist-packages/staticgenerator/__init__.py", line 232, in quick_publish
    return StaticGenerator(*resources).publish()
  File "/dist-packages/staticgenerator/__init__.py", line 229, in publish
    return self.do_all(self.publish_from_path)
  File "/dist-packages/staticgenerator/__init__.py", line 223, in do_all
    return [func(path) for path in self.resources]
  File "/dist-packages/staticgenerator/__init__.py", line 187, in publish_from_path
    filename, directory = self.get_filename_from_path(path)
  File "/dist-packages/staticgenerator/__init__.py", line 179, in get_filename_from_path
    filename = self.fs.join(self.web_root, path.lstrip('/')).encode('utf-8')
  File "/dist-packages/staticgenerator/filesystem.py", line 38, in join
    return os.path.join(paths[0], *[path.lstrip("/") for path in paths[1:]])
  File "/usr/lib/python2.6/posixpath.py", line 67, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'tuple' object has no attribute 'endswith'

【问题讨论】:

    标签: python html django static


    【解决方案1】:

    好的,通过你的跟踪,我猜你的 settings.WEB_ROOT 是一个元组。

    相关行:

    # web root set
    self.web_root = getattr(self.settings, 'WEB_ROOT')
    
    # web root used 
    filename = self.fs.join(self.web_root, path.lstrip('/')).encode('utf-8')
    
    # breaks on real os.path.join
    return os.path.join(paths[0], *[path.lstrip("/") for path in paths[1:]])
    

    【讨论】:

    • 哇,这是一个愚蠢的错误。我在最后用逗号写了 WEB_ROOT。让我绊倒的总是标点符号。 :-) 谢谢!我希望我能投票给你,但声誉系统将我拒之门外。
    • 太棒了!很高兴你发现了问题。 PS:嘘。还不能接受?我想知道是不是因为我有一次删除了帖子。
    • 谢谢 :) 那么它在 upvote 上说了什么?在这一点上我只是好奇,因为我从来没有被阻止投票。
    • 投票需要 15 声望
    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 2023-03-10
    • 2018-06-12
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    相关资源
    最近更新 更多