Artsu
Artsu

Reputation: 23

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

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!

Upvotes: 2

Views: 777

Answers (1)

user1106551
user1106551

Reputation:

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

Upvotes: 4

Related Questions