0% found this document useful (0 votes)
608 views21 pages

Django Blog Setup Guide

This document provides instructions for setting up a simple blog application using Django. It includes steps for installing Django, creating a blog application, setting up the admin interface, adding models, views, templates and forms. Pagination is also implemented to display a list of blog posts. The key steps are: 1. Creating a Django project and blog application 2. Defining a Post model with fields like title, content, author etc 3. Configuring the admin interface to manage blog posts 4. Adding views and templates to display blog posts and detail pages 5. Creating forms to add and update blog posts 6. Adding pagination to display lists of posts

Uploaded by

ivan sugiarto
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
608 views21 pages

Django Blog Setup Guide

This document provides instructions for setting up a simple blog application using Django. It includes steps for installing Django, creating a blog application, setting up the admin interface, adding models, views, templates and forms. Pagination is also implemented to display a list of blog posts. The key steps are: 1. Creating a Django project and blog application 2. Defining a Post model with fields like title, content, author etc 3. Configuring the admin interface to manage blog posts 4. Adding views and templates to display blog posts and detail pages 5. Creating forms to add and update blog posts 6. Adding pagination to display lists of posts

Uploaded by

ivan sugiarto
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Django 1.2.3 on python 2.

7 and mysql
Making Simple Blog

By ivan sugiarto
ivan@[Link]
Setting up Django
 Go to directory on where your project will be created
 Write command (on shell or command prompt) django-
[Link] startproject testsite, this will create
folder containing the project and files that are needed to make
project and don’t forget to make admin account
 Issue command python [Link] runserver on
testsite folder to test the Django installation
 Set up your database in [Link] located in
testsite folder
 Issue python [Link] syncdb to test your
configuration
Creating An Application/Module
 We will create blog module
 Go to testsite directory and type command python [Link] startapp blog
 Go to blog folder and open [Link]
 Write from [Link] import models and from
[Link] import User on top
 Write
class Post([Link]):
author = [Link](User)
date = [Link]()
title = [Link](max_length=100)
post = [Link]()
#making the list title so python not displaying raw data
def __str__(self):
return [Link]
 Edit [Link] and add test [Link] in the INSTALLED_APPS section
 Run command python [Link] syncdb to update database
Make Admin Page
 Open [Link] and add [Link]
 type the command python [Link] syncdb
 Edit [Link] to enable admin, add
from [Link] import admin
[Link]()
urlpatterns = patterns('',
(r'^admin/', include([Link])),
)

 Issue the command python [Link] runserver and check


[Link] to ensure your admin is working, if
you are asked for login then you are on the right path
Make admin page for blog
 Create [Link] on blog directory and write
from [Link] import admin
from [Link] import Post

class PostAdmin([Link]):
#makin order by
date_hierarchy = 'date‘
#making list display field
list_display = ('author', 'date', 'title')
#making search on the top corner
search_fields = ('title', 'post')
#making filter on the right corner
list_filter = ('author', 'date')
#register on the admin page
[Link](Post, PostAdmin)
 Run server (python [Link] runserver) and go to
[Link]
Admin preview
[Link]
 Settings on root [Link] (my best practice though :D)
(r'^blog/', include('[Link]')),

 Blog is the root, and it will include [Link] in the blog folder
 [Link] in blog folder will contain this for example
#([Link]
(function name in [Link])), will be coded as follows
(r'^detail/(?P<post_id>\d+)/$', detail),
Templating
 Create directory on test site called template and edit
[Link]
 Add this following
TEMPLATE_DIRS = (
‘path_to_your_development_folder/testsite/templates’,
)
 On blog/[Link] add this following
from [Link] import get_template
from [Link] import Context, loader
from [Link] import render_to_response, get_object_or_404

def detail(request, post_id):


p = get_object_or_404(Post, pk=post_id)
return render_to_response('blog/[Link]', {'post': p},
context_instance=RequestContext(request))
 Create [Link] in template/blog/ (continued)
Create [Link]
 Type this
<h2>{{ post }}</h2>
<p>{{ [Link] }}</p>
<b>{{ [Link] }}</b> wrote {{ [Link]|wordcount
}} words on {{ [Link]|date }} at {{ [Link]|time
}}

<ul>
<li><a href='/blog/update/{{ [Link] }}'>
edit</a></li>
<li><a href='/blog/delete/{{ [Link] }}'>
delete</a></li>

</ul>
 Cek link
[Link]
Pagination (1)
 Use admin interface to make posts
 Create template in template/blog/[Link]
 Add url pattern in blog/[Link] so it became like this
from [Link] import *
from [Link] import Post
from [Link] import *
urlpatterns = patterns('[Link].date_based',
(r'^add', add_blog
),

(r'^list/(?P<page>\d+)/$', list),

(r'^detail/(?P<post_id>\d+)/$', detail),

)
Pagination(2)
 To add pagination, edit blog/[Link], first add the following lines
from [Link] import Paginator
 Next make the list function like this
def list(request, page = 1):
page = int(page)
post_list = Paginator([Link](), 5, allow_empty_first_page=True)
post_page = post_list.page(page)
num = post_page.start_index()
count = post_page.end_index()
has_previous = post_page.has_previous()
has_next = post_page.has_next()
return render_to_response(
'blog/[Link]',
{
'post_list': post_page.object_list,
'has_previous': has_previous,
'previous_page': page - 1,
'has_next': has_next,
'next_page': page + 1,
'page' : page,
'num' : num,
'count' : count
}
)
Pagination (3) create the template
 Create In template/blog/[Link], and write the following
{% if post_list %}
{{ page }}
{{ num }}
{{ count }}
<a href='/blog/add/'> add </a>
<ul>
{% for post in post_list %}
<li><a href='/blog/detail/{{ [Link] }}'>
{{[Link]}}</a></li>
{% endfor %}
</ul>
{% if has_previous %}
<a href='/blog/list/{{ previous_page }}'>Previous</a>
{% if has_next %} | {% endif %}
{% endif %}
{% if has_next %}
<a href='/blog/list/{{ next_page }}'>Next</a>
{% endif %}
{% else %}
<p>No links found.</p>
{% endif %}
Check blog/[Link]
 Before continuing, please make sure that the [Link] looks like this
from [Link] import *
from [Link] import Post
from [Link] import *

urlpatterns = patterns('',

(r'^add', add_blog
),
(r'^list/(?P<page>\d+)/$', list),
(r'^detail/(?P<post_id>\d+)/$', detail),
(r'^update/(?P<post_id>\d+)/$', update),
(r'^delete/(?P<post_id>\d+)/$', delete)
)
Creating Form Create (1) The Model
 In blog/[Link] add this
#top
from [Link] import ModelForm
#under class post
class PostForm(ModelForm):
class Meta:
model = Post
#the fields on the form, you can add fields, but must
corespond with field in Post
fields = ('title', 'post')
Creating Form Create (2) The View Top
 Add this lines
#top
from [Link] import HttpResponse,
HttpResponseRedirect
from [Link] import render_to_response,
get_object_or_404
from [Link] import get_template
from [Link] import Context, loader,
RequestContext
from [Link] import *
from [Link] import User
from datetime import datetime
from [Link] import
csrf_protect
from [Link] import Paginator
Creating Form Create (2) The View Bottom
#bottom
def add_blog(request):
form = PostForm()

if [Link] == 'POST':
form = PostForm([Link])
if form.is_valid():
form = PostForm([Link])
blog = [Link](commit=False)
[Link] = [Link](id = [Link])
[Link] = [Link]()
[Link]()
return HttpResponseRedirect("/blog/list/1")

else:
return render_to_response('blog/add_blog.html', {'error': True,
'form': form})
else:
return render_to_response('blog/add_blog.html', {'error': True,
'form': form})
Creating Form Create (3) The Template
<html>
<head>
<title>blog</title>
</head>
<body>
{% if error %}
<p style="color: red;">Please submit a blog.</p>
{% endif %}
{% csrf_token %}
<form action="" method="post">
<table>
{{ form.as_table }}
</table>
<input type="submit" value ="submit">
</form>
</body>
</html>
Creating Form Update (1) The View
 Add this code
def update(request, post_id):
post = [Link](pk=post_id)

if [Link] == 'POST':
form = PostForm([Link], instance=post)
if form.is_valid():
[Link]()
return HttpResponseRedirect("/blog/list/1")

else:
return render_to_response('blog/add_blog.html',
{'error': True, 'form': form})
else:
form = PostForm(instance=post)
return render_to_response('blog/add_blog.html',
{'error': True, 'form': form})
Creating Form Update (2) The Template
<html>
<head>
<title>blog</title>
</head>
<body>
{% if error %}
<p style="color: red;">Please submit a blog.</p>
{% endif %}
{% csrf_token %}
<form action="" method="post">
<table>

{{ form.as_table }}

</table>
<input type="submit" value ="submit">
</form>
</body>
</html>
Creating Form Delete (1) The View
def delete(request, post_id):
p = [Link](pk=post_id)
[Link]()
return HttpResponseRedirect("/blog/list/1")
Misc
 You can grab the files in
[Link]

You might also like