Saturday, August 28, 2010

Django Application : Create blog

Django : A Simple Blog Example

  First Create application in Django project directory
#python manage.py startapp blog
 Edit your setting.py file and add appilcation code 
For example :-
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'mysite.blog',  
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
)
also add  Email host to send email.
by default its for locahost, but if your host have not configure EMAIL_HOST then you add this in settings.py 

#EMAIL_HOST = "hostname.com"

 Edit Urls in url.py main file in  project Directory.
Urls.py 
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^blog/', include('mysite.blog.urls')),
)
Edit model.py file in blog directory
Model.py 
from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from django.core.mail import send_mail


class Post(models.Model):
    title = models.CharField(max_length=60)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.title


class Comment(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=60)
    body = models.TextField()
    post = models.ForeignKey(Post)

    def __unicode__(self):
        return unicode("%s: %s" % (self.post, self.body[:60]))

    def save(self, *args, **kwargs):
        """Email when a comment is added."""
        if "notify" in kwargs and kwargs["notify"] == True:
            message = "Comment was was added to '%s' by '%s': \n\n%s" % (self.post, self.author,
                                                                         self.body)
            from_addr = "no-reply@example.com"
            recipient_list = ["jagdeepmalhi@gndec.ac.in"]
            send_mail( "New comment added", message, from_addr, recipient_list)

        if "notify" in kwargs: del kwargs["notify"]

        super(Comment, self).save(*args, **kwargs)

 Create one more file urls.py file in your blog directory and edit
Urls.py 
from django.conf.urls.defaults import *

from mysite.blog.models import *
urlpatterns = patterns('mysite.blog.views',
   (r"^(\d+)/$", "post"),
   (r"^add_comment/(\d+)/$", "add_comment"),
   (r"^delete_comment/(\d+)/$", "delete_comment"),
   (r"^delete_comment/$", "delete_comment"),
   (r"^month/(\d+)/(\d+)/$", "month"),
   (r"", "main"),
)

Create the admin.py file in blog directory and edit file for admin Interface.
Admin.py 
from mysite.blog.models import *
from django.contrib import admin

class PostAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ("title", "created")
    list_filter =  ['title']

class CommentAdmin(admin.ModelAdmin):
    list_display = ["post", "author", "created"]
    list_filter =  ['created']
   
admin.site.register(Post, PostAdmin)
admin.site.register(Comment, CommentAdmin)

Edit the Views.py file for create the user view.
Views.py
# Create your views here.
import time
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf
from django.core.paginator import Paginator, InvalidPage, EmptyPage

from mysite.blog.models import *
from django.forms import ModelForm

class CommentForm(ModelForm):
    class Meta:
        model = Comment
        exclude = ["post"]


def post(request, pk):
    """Single post with comments and a comment form."""
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)
    d = dict(post=post, comments=comments, form=CommentForm(), user=request.user)
    d.update(csrf(request))
    return render_to_response("blog/post.html", d)

def delete_comment(request, pk=None):
    """Delete comment(s) with primary key `pk` or with pks in POST."""
    if request.user.is_staff:
        if not pk: pklst = request.POST.getlist("delete")
        else: pklst = [pk]

        for pk in pklst:
            Comment.objects.filter(pk=pk).delete()
        return HttpResponseRedirect(request.META["HTTP_REFERER"])
  
def add_comment(request, pk):
    """Add a new comment."""
    p = request.POST

    if p.has_key("body") and p["body"]:
        author = "Anonymous"
        if p["author"]: author = p["author"]
        comment = Comment(post=Post.objects.get(pk=pk))

        # save comment form
        cf = CommentForm(p, instance=comment)
        cf.fields["author"].required = False
        comment = cf.save(commit=False)

        # save comment instance
        comment.author = author
        notify = True
        if request.user.username == "ak": notify = False
        comment.save(notify=notify)
    return HttpResponseRedirect("/django/blog/%s/" % pk)

def mkmonth_lst():
    """Make a list of months to show archive links."""
    if not Post.objects.count(): return []

    # set up vars
    year, month = time.localtime()[:2]
    first = Post.objects.order_by("created")[0]
    fyear = first.created.year
    fmonth = first.created.month
    months = []
    mnames = "January February March April May June July August September October November December"
    mnames = mnames.split()

    # loop over years and months
    for y in range(year, fyear-1, -1):
        start, end = 11, -1
        if y == year: start = month
        if y == fyear: end = fmonth-1

        for m in range(start, end, -1):
            months.append((y, m, mnames[m]))
    return months

def month(request, year, month):
    """Monthly archive."""
    posts = Post.objects.filter(created__year=year, created__month=month)
    for post in posts:
        post.num_comments = Comment.objects.filter(post=post).count()
    return render_to_response("blog/list.html", dict(post_list=posts, user=request.user,
                                                months=mkmonth_lst(), archive=True))

def main(request):
    """Main listing."""
    posts = Post.objects.all().order_by("-created")
    paginator = Paginator(posts, 2)
    try: page = int(request.GET.get("page", '1'))
    except ValueError: page = 1

    try:
        posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        posts = paginator.page(paginator.num_pages)

    for post in posts.object_list:
        post.num_comments = Comment.objects.filter(post=post).count()

    return render_to_response("blog/list.html", dict(posts=posts, user=request.user,
                                                post_list=posts.object_list, months=mkmonth_lst()))

Now Create the Templates for User and place these file in your templates Directory.
list.html

{% extends "blog/bbase.html" %}


{% block sidebar %}
    <style type="text/css">
        #sidebar { float: right; border: 1px dotted #ccc; padding: 4px; }
        a { margin-left: 15px; }
    </style>
    <div id="sidebar">
        Monthly Archive
        <p>
        {% for month in months %}
            {% ifchanged month.0 %} {{ month.0 }} <br /> {% endifchanged %}
            <a href="/django/blog/month/{{ month.0 }}/{{ month.1 }}/">{{ month.2 }}</a> <br />
        {% endfor %}
        </p>
    </div>
{% endblock %}


{% block content %}
    <style type="text/css">
        .main { margin-left: 25px; margin-top: 30px; }
        .time { font-size: 0.8em; margin-top: 2px; }
        .body { font-size: 1.1em; margin-top: 2px; }
        .commentlink { text-align: right; }
        .step-links a { font-size: 0.89em; }
        .title {
            font-size: 1.4em; margin-top: 20px; border-bottom: 1px solid #ccc;
            padding-left: 4px;
        }
        .messages { margin-left: 20px; }
    </style>
    <div class="main">


        <!-- Messages  -->
        {% if messages %}
        <ul class="messages">
            {% for message in messages %}
            <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
        </ul>
        {% endif %}


        <!-- Posts  -->
        <ul>
            {% for post in post_list %}
                <div class="title">{{ post.title }}</div>
                <ul>
                    <div class="time">{{ post.created }}</div>
                    <div class="body">{{ post.body|linebreaks }}</div>
                    <div class="commentlink">
                        <a href="/django/blog/{{ post.pk }}/">Comments ({{ post.num_comments }})</a></div>
                </ul>
            {% endfor %}
        </ul>


        <!-- Next/Prev page links  -->
        {% if not archive and posts.object_list and posts.paginator.num_pages > 1 %}
        <div class="pagination" style="margin-top: 20px; margin-left: -20px; ">
            <span class="step-links">
                {% if posts.has_previous %}
                    <a href= "?page={{ posts.previous_page_number }}">newer entries &lt;&lt; </a>
                {% endif %}


                <span class="current">
                    &nbsp;Page {{ posts.number }} of {{ posts.paginator.num_pages }}
                </span>


                {% if posts.has_next %}
                    <a href="?page={{ posts.next_page_number }}"> &gt;&gt; older entries</a>
                {% endif %}
            </span>
        </div>
        {% endif %}


    </div>


{% endblock %}



bbase.html 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <title>{% block title %}Blog{% endblock %}</title> </head>


<body>
    <div id="sidebar"> {% block sidebar %} {% endblock %} </div>
    <div id="container">
        <div id="menu">
            {% block nav-global %}


                <!-- MENU -->
                <h3>Jagdeep Singh</h3>
                {% if user.is_staff %}
<a href="/django/admin/">Admin</a> <a href="/django/admin/blog/post/add/">Add post</a>          
              {% endif %}
            {% endblock %}
        </div>
        <div id="content">
            {% block content %}{% endblock %}
        </div>
    </div>
</body>
</html>




post.html 

{% extends "blog/bbase.html" %}

{% block content %}
    <style type="text/css">
        .main { margin-left: 25px; margin-top: 30px; }
        .time { font-size: 0.8em; margin-top: 2px; }
        .comment { border: 1px solid #eee; padding: 4px; margin-bottom: 12px; }
        .body { font-size: 1.1em; margin-top: 2px; }
        .commentlink { text-align: right; }
        .step-links a { font-size: 0.89em; }
        .title {
            font-size: 1.4em; margin-top: 20px; border-bottom: 1px solid #ccc;
            padding-left: 4px;
        }
        .messages { margin-left: 20px; }

        #id_body { width: 500px; height: 250px; }
        #addc {
            width: 508px; background: #f5f5f5; padding: 3px; padding-bottom: 8px;
            border-bottom: 1px dotted #aaa; }
        #cform { background: #f5f5f5; width: 506px; padding: 4px; padding-bottom: 0px; }
        #submit { text-align: right; margin-right: 50px; width: 500px; margin-top: -5px; }

        ul { margin-bottom: 30px; }
    </style>
    <div class="main">

        <!-- Messages  -->
        {% if messages %}
        <ul class="messages">
            {% for message in messages %}
            <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
            {% endfor %}
        </ul>
        {% endif %}

        <!-- Post  -->
        <ul>
            <div class="title">{{ post.title }}</div>
            <ul>
                <div class="time">{{ post.created }}</div>
                <div class="body">{{ post.body }}</div>
            </ul>
            <!-- Comments  -->
            {% if comments %}
                <p>Comments:</p>
            {% endif %}

            <form action="/django/blog/delete_comment/" method="POST">{% csrf_token %}
            {% for comment in comments %}
                <div class="comment">
                    <div class="time">{{ comment.created }} | {{ comment.author }}</div>
                    <div class="body">{{ comment.body|linebreaks }}</div>
                    {% if user.is_staff %}
                        <input type="checkbox" name="delete" value="{{ comment.pk }}">
                        <a href="/django/blog/delete_comment/{{ comment.pk }}/">delete</a>
                    {% endif %}
                </div>
            {% endfor %}

            {% if user.is_staff and comments %}
                <p><input type="submit" value="Delete all selected"></p>
                <br />
            {% endif %}
            </form>

            <div id="addc">Add a comment</div>
            <!-- Comment form  -->
            <form action="/django/blog/add_comment/{{ post.pk }}/" method="POST">{% csrf_token %}
                <div id="cform">
                    Name: {{ form.author }}
                    <p>{{ form.body|linebreaks }}</p>
                </div>
                <div id="submit"><input type="submit" value="Submit"></div>
            </form>
        </ul>

    </div>

{% endblock %}

Now enjoy your  Blog on Django.

1 comment:

  1. You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site.
    Tropic Diva

    ReplyDelete