EmbLogic's Blog

Make your first BLOG using django tool (PYTHON)

BUILD up your first BLOG site using Django tool

 

STEP 1:

Install Python 2.7 or above and django

 

STEP 2:

write this command to create your project with default settings and server – python ‘django location’ startproject project_name

 

python /home/embkm/Django-1.6.5/django/bin/django-admin.py startproject testsite

 

now you will find a new folder named ‘testsite’ in your pwd

 

 

STEP 3:

MOVE to testsite folder – cd testsite/

then open settings.py -vim settings.py

& update data as:

 

DATABASES = {

‘default’: {

‘ENGINE’: ‘django.db.backends.sqlite3′,

‘NAME’: ‘site.db’, //write any name :it is your database file which will automatically be formed in your project folder

}

}

 

STEP 4:

 

check 'django.contrib.admin', is added in installed_apps

 

if not then add it or uncomment it if commented

it enable admin functionality in our project

 

in urls.py uncomment lines :

 

from django.contrib import admin

admin.autodiscover()

url(r'^admin/', include(admin.site.urs)),

 

 

STEP 5:

 

 

first come to project directory - cd ..

this command tells django to translate app files into real database & create necessary tables :

python manage.py syncdb

after this step you will be asked to create a admin ,give user name,password,email id ,

STEP 6:

now we will start the server :

python manage.py runserver

open browser and open

http://127.0.0.1

& then

http://127.0.0.1/admin

now login with your admin id & password

STEP 7:

Now stop server using ctrl+c

lets start making our blog :

in project directory type:

python manage.py startapp blog

which create blog directory which basic application functionalities

STEP 8:

Now go to blog : cd blog/

vim models.py

& add create your model :

from django.db import models

from taggit.managers import TaggableManager //used to add tags in our project

class Post(models.Model):

title = models.CharField(max_length=100);

body = models.TextField()

created = models.DateTimeField() //date of creation

tags = TaggableManager()

def __unicode__(self):

return self.title

install tags using pip installl django-taggit in command window

STEP 9:

create admin.py in blog and write:

from django.contrib import admin

from blog.models import Post

admin.site.register(Post)

STEP 10:

add blog in your project setting

open testsite

vim setting.py

in INSTALLED_APPS

add 'taggit' & 'blog'

in project folder run:

python manage syncdb

python manage runserver

now open http://127.0.0.1/blog

you will find posts & tag in it

goto posts & now you can add posts to your site

STEP 10:

Now we will make our website visible

vim urls.py in testsite

& edit it as :

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',

# Examples:

# url(r'^$', 'testsite.views.home', name='home'),

url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),

STEP 11 :

In blog folder create a file urls.py

vim urls.py

& edit it with :

from django.conf.urls import patterns, include, url

from django.views.generic import ListView,DetailView

from blog.models import Post

from django.contrib.syndication.views import Feed

class BlogFeed(Feed):

title='mysite'

description='some info of mine'

link='/blog/feed/'

def items(self):

return Post.objects.all().order_by("-created")[-2]

def item_title(self,item):

return item.title

def item_description(self,item):

return item.body

def item_link(self,item):

return u"/blog/%d" % item.id

urlpatterns = patterns('blog.views',

url(r'^$',ListView.as_view(queryset=Post.objects.all().order_by("-created")[:2],

template_name="blog.html")),

url(r'^(?P<pk>\d+)$',DetailView.as_view(

model=Post,

template_name="post.html")),

url(r'^archives/$',ListView.as_view(queryset=Post.objects.all().order_by("-created"),

template_name="archives.html")),

url(r'^tag/(?P<tag>\w+)$','tagpage'),

url(r'^feed/$',BlogFeed()),

)

STEP 12:

Create a folder 'templates' in blog

make four files in it:blog.html (index page),base.html,archives.html,tagpage.html,post.html

and write following code in them :

blog.html :

{% extends 'base.html' %}

{% block content %}

{% for post in object_list %}

<h2><a href="/blog/{{post.id}}">{{ post.title }}</a></h2>

<div class='post_meta'>

on {{post.created}}

</div>

<div class='post_body'>

{{post.body|safe|linebreaks}}

</div>

<div class ='tags'>

{% for tag in post.tags.all %}

<a href="/blog/tag/{{tag}}">{{tag}}</a>

{% if not forloop.last %},

{% endif %}

{% endfor %}

</div>

{% endfor %}

{% endblock %}

base.html:

<h1>My site yoyo </h1>

{% block content %}

{% endblock %}

post.html:

{% extends 'base.html' %}

{% block content %}

<h2>{{ post.title }}</h2>

<div class='post_meta'>

on {{post.created}}

</div>

<div class='post_body'>

{{post.body|safe|linebreaks}}

</div>

<div class ='tags'>

{% for tag in post.tags.all %}

<a href="/blog/tag/{{tag}}">{{tag}}</a>

{% if not forloop.last %},

{% endif %}

{% endfor %}

</div>

{% endblock %}

tagpage.html:

{% extends 'base.html' %}

{% block content %}

<h2>Posts tagged: {{tag}}</h2>

{% for post in posts %}

<p>{{post.created|date:"Y-m-d"}}:<a href="/blog/{{post.id}}">{{ post.title }}</a></p>

{% endfor %}

{% endblock %}

archives.html:

{% extends 'base.html' %}

{% block content %}

{% for post in object_list %}

<p>{{post.created|date:"Y-m-d"}}:<a href="/blog/{{post.id}}">{{ post.title }}</a></p>

{% endfor %}

{% endblock %}

STEP 13:

Now we are ready to run our blog .

Start server

python manage.py runserver

go to browser & open

http://127.0.0.1/blog

& now enjoy your first blog using python 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>