如何开始形成一个django网站和如何django结构页面? [英] How to start forming a django website and how django structures pages?

查看:110
本文介绍了如何开始形成一个django网站和如何django结构页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个django项目我的个人网站学习django。到目前为止,我已经将我的开发环境设置为我需要的一切,并遵循这个伟大的教程来创建一些基本的数据结构和模板。现在我想开始使用我之前制作的html布局,并开始实现它的功能。但是,我很难理解如何完成这一点。



在此之前,我主要完成了Java门户解决方案,我可以启动服务器,创建一些页面,为其设置我的主题,然后添加自定义portlet(功能/代码)无论我想要什么在django中,我创建了一个我的html布局的视图,它的工作正常。但是我不明白我应该如何处理页面。我需要为每个url创建单独的视图并查看配置,然后为每个页面使用相同的html布局,并且只需在需要的地方设置不同的功能?不像django的方式,因为少了更多。我想我可以使用视图有点像portlet,以便它们是某些页面的一部分(这是一个视图本身?)对吗?像一个页面某一部分的投票。怎么样?



所以例如我想有一个首页,我有一个主题(html-layout),也许最新的博客文章。然后另一个名为资源的页面,其中有相同的主题和可下载内容的列表。自然地在布局中的某个导航页面之间切换。



如果有人可以指向正确的方向,我将非常有责任感。

解决方案

可以使用Django模板继承来做到这一点。你将做一个家模板,并使用 {%block content%} {%endblock%} 来定义博客文章的位置。在您将显示博客文章(这是一个单独的HTML文件)的模板中,您将将 {%extendshome.html%} 和相同的 {%block content%} {%endblock%} ,以及 {%block content%} {%endblock%} 你将把HTML代码放在你的博客文章中。



当Django加载主页时,它会在其他模板中看到其中名为content的块,将加载其中的任何内容,在这种情况下,您的博客文章。



让我向您展示一个例子:



home.html

 < title>首页< / title> 
< head>
< style type =text / css>
.content-wrapper {
text-decoration:none;
border:1px none;
height:50%;
剩余:0%;
position:relative;
顶部:8%;
width:100%;
height:100%;
z-index:0;
}
< / style>
< / head>
< body>
< div class =content-wrapper>
{%block content%} {%endblock%}
< / div>
< / body>

和blog-posts.html

  {%extendshome.html%} 
< head>这里所有的CSS样式您将被使用,并将它们放在HOME.HTML< / head>
< body>
{%block content%}
您的博客的HTML
{%endblock%}
< / body>

这样,您将不得不单独执行模板,但会减少代码。而且你必须将它们指向urls.py,因为它们的行为几乎像框架。



urls.py

  urlpatterns = patterns('',
url(r'^ blog-posts /','Mod031.views.blog-posts'),
url (r'^ home /','Mod031.views.home',name ='home'),

您还将拥有加载您需要的每个模板的视图

  def home(request) :
return render_to_response('home.html',context_instance = RequestContext(request))

def blog-posts(request):
return render_to_response('blog-posts.html ',context_instance = RequestContext(request))

有关更多信息,请阅读文档: Django模板语言


I started a django project for my personal website to learn django. So far I've got my development environment set with everything I need and followed this great tutorial to create some basic data structures and templates. Now I would like to start using my html layout I made before and start implementing the functionalities to it. However I'm having hard time understanding how to accomplish this.

I've mostly done java portal solutions before this where I could start the server, create some pages, set my theme for them and then add custom portlets (the functionalities/code) wherever I wanted. In django, I've created a view with my html layout and it works fine. I however don't understand how I should handle pages. Do I need to create a separate view for each in url and view configs and then use the same html layout for each page and only set different functionalities wherever needed? Doesn't seem like django way since less is more. I guess I can use the views somehow a bit like portlets so that they are part of some page (which is a view itself?) right? Like a poll in a certain part of a page. How?

So for example I want to have a frontpage where I have a certain "theme" (html-layout) and maybe latest blog posts there. Then another page named resources where there is the same "theme" and a list of downloadable content. Naturally a navigation somewhere in the layout to switch between pages.

I'd be much obliged if someone could point me to the right direction!

解决方案

You can do that by using Django template inheritance. You will do a "home" template, and will use {% block content %}{% endblock %} to define where the blog posts will be. In the template where you will display the blog posts (which is a separate HTML file), you will put {%extends "home.html"%} and the same {% block content %}{% endblock %}, and inside {% block content %}{% endblock %}, you will put the HTML code for your blog posts.

When Django loads the home page, it will look where else you have that block named "content" in other templates and will load whatever is inside them, which in this case, is your blog posts.

Let me show you a little example:

home.html

<title>Home</title>
<head>
<style type="text/css">
    .content-wrapper{
        text-decoration:none;
    border: 1px none;
        height: 50%;
        left: 0%;
        position: relative;
        top: 8%;
        width: 100%;
        height: 100%;
        z-index: 0;
     }
</style>
</head>
<body>
<div class="content-wrapper">
{% block content %}{% endblock %}
    </div>
</body>

and blog-posts.html

{%extends "home.html"%}
 <head>PUT HERE ALL THE CSS STYLESHEETS YOU'LL BE USING AND PUT THEM ALSO IN HOME.HTML</head>
<body>
   {%block content%}
      HTML FOR YOUR BLOG POSTS
   {%endblock%}
</body>

This way you will have to do separate templates, but will do much less code. And you will have to point them in urls.py, because they will behave almost like frames.

urls.py

urlpatterns = patterns('',
url(r'^blog-posts/','Mod031.views.blog-posts'),
    url(r'^home/', 'Mod031.views.home', name='home'),
)

You will have, also, a view to load each template you need

def home (request):
return render_to_response('home.html', context_instance=RequestContext(request))

def blog-posts(request):
return render_to_response('blog-posts.html', context_instance=RequestContext(request))

For further information, please, read the docs: The Django template language

这篇关于如何开始形成一个django网站和如何django结构页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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