存储在数据库中的Django站点的URL [英] URLs stored in database for Django site

查看:116
本文介绍了存储在数据库中的Django站点的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成了一些Django网站,直到现在我已经在urls.py中映射单个视图和URL。

I've produced a few Django sites but up until now I have been mapping individual views and URLs in urls.py.

现在我试图创建一个小型自定义CMS,但我遇到了URL的问题。我有一个数据库表(SQLite3),其中包含页面的代​​码,如一个标题列,一个用于右菜单,一个用于内容....等等。我还有一个URL列。如何让Django从列中存储的URL调用数据库表中的信息,而不是为每个页面编写视图和URL(这显然会违反CMS的目的)?

Now I've tried to create a small custom CMS but I'm having trouble with the URLs. I have a database table (SQLite3) which contains code for the pages like a column for header, one for right menu, one for content.... so on, so on. I also have a column for the URL. How do I get Django to call the information in the database table from the URL stored in the column rather than having to code a view and the URL for every page (which obviously defeats the purpose of a CMS)?

如果有人可以指向我正确的文档部分或一个网站,解释这将有很多帮助。

If someone can just point me at the right part of the docs or a site which explains this it would help a lot.

谢谢所有。

推荐答案

你不必在flatpage-way

You dont have to to it in the flatpage-way

对于模型,这应该是可寻址的,我这样做:

For models, that should be addressable, I do this:

在urls.py我有一个url映射如

In urls.py I have a url-mapping like

  url(r'(?P<slug>[a-z1-3_]{1,})/$','cms.views.category_view', name="category-view")



在这种情况下,正则表达式 P [a-z1-3 _] {1,})将返回一个名为slug的变量并将其发送到我的视图 cms.views.category_view 。在这个视图中,我这样查询:

in this case the regular expression (?P<slug>[a-z1-3_]{1,}) will return a variable called slug and send it to my view cms.views.category_view. In that view I query like this:

@render_to('category.html')
def category_view(request, slug):
    return {'cat':Category.objects.get(slug=slug)}

(注意:我使用烦人装饰 render_to - 它与 render_to_response 相同,只是更短)

(Note: I am using the annoying-decorator render_to – it is the same as render_to_response, just shorter)

修改:这应该由教程涵盖。在此处,您可以找到网址配置和调度 a>每一个细节。 djangobook也覆盖了它。并检查pythons regex模块。

Edit This should be covered by the tutorial. Here you find the url-configuration and dispatching in every detail. The djangobook also covers it. And check pythons regex module.

当然你可以使用这段代码。

Of course you can use this code.

这篇关于存储在数据库中的Django站点的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆