python Django - 网站主页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - Website Home Page
提问by Peter
I've been having a look at Django and, from what I've seen, it's pretty darn fantastic. I'm a little confused, however, how I go about implementing a "home page" for my website? Would it be a separate app, or just a view within the project, or what?
我一直在研究 Django,就我所见,它非常棒。我有点困惑,但是,我如何为我的网站实施“主页”?它是一个单独的应用程序,还是项目中的一个视图,还是什么?
采纳答案by SingleNegationElimination
There's no real rule for this, But one thing I like to do is actually arrange for the index access to redirect to another spot. If you prefer, though, you can just give the index page a plain view.
对此没有真正的规则,但我喜欢做的一件事实际上是安排索引访问重定向到另一个位置。但是,如果您愿意,您可以只为索引页面提供一个简单的视图。
That said, It's probably a good idea to keep all your code in an actual app, so that you can refactor it more easily, and so that it appears on the python path as a normal module. Putting views in the project rather than an app seems to cause more headaches than it solves.
也就是说,将所有代码保存在实际应用程序中可能是个好主意,这样您就可以更轻松地对其进行重构,并使其作为普通模块出现在 python 路径中。将视图放在项目中而不是应用程序中似乎比它解决的更令人头疼。
回答by bnjmn
I just found my original approach (direct_to_template
) is deprecated in Django 1.5
我刚刚发现我的原始方法 ( direct_to_template
) 在 Django 1.5 中已被弃用
Instead, use a TemplateView
to achieve the same result
相反,使用 aTemplateView
来实现相同的结果
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^$',
TemplateView.as_view(template_name='index.html'),
name='index'),
)
(For Django 1.4) You can setup a direct_to_template
url within ./project/project/urls.py
(对于 Django 1.4)你可以在里面设置一个direct_to_template
url./project/project/urls.py
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^$', direct_to_template, { 'template': 'index.html'}),
# add urls to apps here
)
For both, place the template (index.html
) in your TEMPLATE_DIRS
root. This is one approach to create a homepage without implementing an entire app. There are many ways to make this happen as others have noted.
对于两者,将模板 ( index.html
) 放在您的TEMPLATE_DIRS
根目录中。这是一种无需实现整个应用程序即可创建主页的方法。正如其他人所指出的,有很多方法可以实现这一点。
回答by Anthony Lewis
The easiest way is using Django's "Flatpages". See this link for more info: http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/
最简单的方法是使用 Django 的“Flatpages”。有关更多信息,请参阅此链接:http: //docs.djangoproject.com/en/dev/ref/contrib/flatpages/