How to start forming a django website and how django structures pages?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 06:16:34

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!