来自 sql 的 django urlpatterns

发布于 2024-10-25 20:56:34 字数 200 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-11-01 20:56:34

继续您对 pyeleven 答案的评论,看来您已经理解了 urlpatterns 的要点。您不需要或不想在 urlconf 中指定您的部分的选择。您要做的就是获取网址每个部分的,并将其作为参数传递给视图。因此,例如:

(r'^?P<section>\w+)/

这将获取 /name1//name2/ 等网址,并传递 name1name2 > 作为 section 参数传递给视图。因此,每当添加部分时都无需更改代码。

, 'my_view')

这将获取 /name1//name2/ 等网址,并传递 name1name2 > 作为 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:

(r'^?P<section>\w+)/

This will grab urls like /name1/ and /name2/, and pass name1 and name2 to the view as the section parameter. So there's no need to change the code whenever you add a section.

, 'my_view')

This will grab urls like /name1/ and /name2/, and pass name1 and name2 to the view as the section parameter. So there's no need to change the code whenever you add a section.

决绝 2024-11-01 20:56:34

虽然这是可以想象到的最恶心、最不符合 django 风格的东西,但如果你真的非常想的话,你可以从数据库获取你的 url:

models.py:

from django.db import models

class Url(models.Model):
    name = models.CharField(max_length=20)

urls.py< /strong>:

from my_app.models import Url

urls = []
for url_object in Url.objects.all():
    urls.append(url(url_object.name, 'my_view'))

urlpatterns = patterns('my_app.views', *urls)

瞧。它确实有效。直接来自数据库的 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:

from django.db import models

class Url(models.Model):
    name = models.CharField(max_length=20)

urls.py:

from my_app.models import Url

urls = []
for url_object in Url.objects.all():
    urls.append(url(url_object.name, 'my_view'))

urlpatterns = patterns('my_app.views', *urls)

Voilà. It actually works. Url patterns directly from the db. Please don't do this.

I'm going to go take a shower now.

半透明的墙 2024-11-01 20:56:34

你检查过 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文