On the last part, we have tried display article post in homepage. And now, we will try implement into detail page. The source code just simple like the last part. Are you ready to write a code?

A. Url

Open urls.py, and add the following script: 

1
2
3
4
5
urlpatterns = patterns('',
    url(r'^$', 'myblog.views.home', name='home'),
    url(r'^detail/(?P<slug>[\w-]+)$', 'article.views.detail'),
    ...
)

 

Explanation (More)

On the last part, we have setting the template. And now, we will get article data from model and show it in template. So, when we access the home url, it will show list the last ten article. On this section, we are focus only on home page. for other page, we will discuss on the next section. Are you ready to write a code? 

A. View

Open views.py in the project directory (parallel with settings.py and urls.py), and change the source code like this:

1
2
3
4
5
6
7
8
from article.models import Category, Post
...
def home(request):
    post = Post.objects.all()[:10]
    data = {
        'article' : post
    }
    return render_to_response('index.html', data, context_instance=RequestContext(request))

 

Explanation (More)

Hi, guys. Now we want to add the template into django. And now, we are using bootstrap for templating. The template that we want to use is default by bootstrap, using Blog Template Example.

Static 

First of all, we create the static directory on the root, and put the bootstrap files on there. All file static, likes an image, an icon, css file, etc. will be located under static directory.

And then create directory called templates, and put the index.html on this directory. All of html file will be under templates directory.

So we will got the structure like this : (More)

As I mentioned in the previews post (Part 3), that django have a admin page, and will generate the admin form from the models. Django admin can be access from url using /admin. If you can't access admin page, please make sure you have activate the admin. To activate the admin page, look at setting.py and url.py.

Make sure, django.contrib.admin added on INSTALLED_APPS in setting.py :

1
2
3
4
5
INSTALLED_APPS = (
    ...
    'django.contrib.admin',
    ...
)

 

Also make sure, that the following code are exist in url.py (More)

On the last tutorial (Part 2), we have create an app, it called article. Now we will create the models to represent database model, about table and field. For your information, that "django will generate the table (with all field) and admin form from the models".

Creating models 

Now, open the models.py in article app:

There are any code on the top of the models: 

1
2
3
from django.db import models
 
# Create your models here.

and now, make the models like this: (More)

In this case, we will create the article app. About the name, is up to you. But I think, content of the blog it called article. So my app name is article. The apps can live anywhere on your Python path. In this tutorial, we’ll create our article app right next to your manage.py file so that it can be imported as its own top-level module.

To create your app, make sure you’re in the same directory as manage.py and type this command: (More)

Description 

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Developed by a fast-moving online-news operation, Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly. 

Installing

The goal of this tutorial is we can build a blog site. First of all, make sure that we have install pip and virtualenvwrapper.

Step 1: Make a virtualenv with name is django_blog or anything you want:  (More)

Installing virtualenvwrapper

| | Add comment | Trackbacks (0)

Description 

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies. 

Installation 

The easiest way to install it is using pip: (More)

Installing PIP

| | Add comment | Trackbacks (0)

Description 

pip is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI). It is important for the future to install any python package. 

Installation 

To install or upgrade pip, first download get-pip.py. After it downloaded, run the following command in terminal "python get-pip.py". (don't forget as administrator access). Pip required setuptools package, if it is not already installed, get-pip.py will automatic install it. (More)