我试图从我的应用程序支持的所有URL的代码自动创建文档.我们使用完全客户端MVC,因此支持的每个URL本质上是UI的REST API.有没有一种从这些URL生成文档的简单方法?
我现在写了这个小模块,但我正在寻找更好的方法.如果类似的东西已经存在,我不想重新发明轮子.
更新:请注意,目的是为网站的消费者提供公共文档,而不是内部消费.在这方面,我们需要记录每个URL: - 响应是什么, - 接受哪些参数, - 如果URL响应GET/POST或两者,等等.
某些只是重定向到主页的URL(^ $)不应该被记录,所以我还需要一些排除机制.
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
''' Generates documentation for the URLs. For each URL includes the documentation from
the callback implementing the URL and prints the default arguments along with the URL pattern and name'''
def handle(self, *args, **options):
url_module = None
exec 'import '+settings.ROOT_URLCONF+'; url_module='+settings.ROOT_URLCONF
doc = file('doc.html','w')
doc.write("<table border=1 cellspacing=0 cellpadding=5>")
doc.write("<tr><th>URL Pattern<th>Name<th>Documentation<th>Parameters")
for x in url_module.urlpatterns:
doc.write("<tr>")
doc.write("<td>")
doc.write(x._regex) …Run Code Online (Sandbox Code Playgroud)