来自 sql 的 django urlpatterns
我正在尝试使用 sql 查询创建 urlpatterns,但这仅适用于服务器启动时 sql 表中已经存在的那些内容。是否可以让 django 从数据库动态检查新的 url?
我知道,这可以用正则表达式来完成,但它们太贪婪了,我的意思是,我需要在我的网站的根级别上进行此操作,正则表达式将“吃掉”所有匹配的名称,并且此正则表达式必须是最后一个 urlpatterns列表。
I am trying to create urlpatterns with sql query, but this will work only for those things that already was in sql table at the moment of server start. If it possible to make django to check for new urls dynamically from database?
I know, this can be done with regular expressions, but they are too greedy, I mean, that i need to make this at root level of my site and regexp will "eat" all matching names and this regexp must be the last of urlpatterns list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
继续您对 pyeleven 答案的评论,看来您已经理解了 urlpatterns 的要点。您不需要或不想在 urlconf 中指定您的部分的选择。您要做的就是获取网址每个部分的值,并将其作为参数传递给视图。因此,例如:
这将获取
/name1/
和/name2/
等网址,并传递name1
和name2
> 作为section
参数传递给视图。因此,每当添加部分时都无需更改代码。Going on your comment to pyeleven's answer, it seems you have understood the point of urlpatterns. You don't need or want to specify the choices of your section in the urlconf. What you do is grab the value of each section of the url, and pass that as a parameter to the view. So, for example:
This will grab urls like
/name1/
and/name2/
, and passname1
andname2
to the view as thesection
parameter. So there's no need to change the code whenever you add a section.虽然这是可以想象到的最恶心、最不符合 django 风格的东西,但如果你真的非常想的话,你可以从数据库获取你的 url:
models.py:
urls.py< /strong>:
瞧。它确实有效。直接来自数据库的 URL 模式。请不要这样做。
我现在要去洗澡了。
Although this is the nastiest, most un-django-esque thing imaginable, you can get your urls from the db, if you really, really want to:
models.py:
urls.py:
Voilà. It actually works. Url patterns directly from the db. Please don't do this.
I'm going to go take a shower now.
你检查过 django 平面页面吗?
http://docs.djangoproject.com/en/dev/ ref/contrib/flatpages/?from=olddocs
动态 url 可能不是一个好主意,例如动态添加的错误 url 行可能会使服务器停止运行。
您能详细说明一下您的目标吗?
Have you checked django flatpages?
http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/?from=olddocs
Dynamic url might not be such a good idea, for example a bad url line added dynamically might make the server stop functioning.
Can you elaborate on your goals?