Saturday, 23 August 2014

user todo list application or sample project using python with django

ok today i will tell you how create application in python using django already i have tell you that python easy and smooth and also i have tell you how you install or create a application with mysql in django in previous article:

and also i have tell you how you create or login using django default user table:

but i know you know all of this but you have any idie how you will start so today i have decide that i will tell you how you create/design a project , i give a sample project like user todo list, Already i have create this project using node.js but here i will create using python with django i think you will enjoy in this project

ok let's start your sample project:
create a database in mysql like todo
first open your terminal and go to that folder where you want to create your folder and type bellow command:

 $ startproject todo

this command create a project todo with a folder structure.
next goto todo folder and type bellow command:

$cd todo
$python startapp app
$python startapp data

it will create two app one for web view and other for database connection
and it will create two folder one app and data

ok next edit and copy bellow all in file
Django settings for store project.

For more information on this file, see

For the full list of settings and their values, see

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^py0denvmw(8&m1i@wi%5-c6_nmk@&!!2)os7pyr#0gr8fc4kv'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True



TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)


# Application definition



ROOT_URLCONF = 'store.urls'

WSGI_APPLICATION = 'store.wsgi.application'

# Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "todo",
        'USER': "root",

# Internationalization



USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC_URL = os.path.join(BASE_DIR, 'todo/static/')

MEDIA_ROOT = os.path.join(BASE_DIR, 'todo/media/')
MEDIA_URL = 'static/media/'

 next open data folder and edit file and copy bellow syntex:

from django.db import models
from datetime import datetime, timedelta

class User(models.Model):
 fname = models.CharField(max_length=255)
 lname= models.CharField(max_length=255)
 password = models.CharField(max_length=200)
 #profile_pic = models.ImageField(upload_to = 'user/',null=True)

class Todo(models.Model):
    todo_job= models.TextField()

 this model for access database and manipulate data in database
and run bellow command from your root folder in terminal

$python syncdb

 next open app folder and delete file and create views folder folder look like this:
open views folder and create file and then create two file and and copy bellow content:
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from import *
from django.template import RequestContext
from django.shortcuts import render_to_response

def main_page(request):
    if 'user_id' in request.session:
        return render_to_response('home.html',data)
        form = SignupForm()
        variables = RequestContext(request, {'form': form})
        return render_to_response('index.html', variables)

def signup(request):
    if request.method=='POST':
        if form.is_valid():
    return HttpResponseRedirect('/')

def login(request):
    if user_obj.count():
        print user_obj
    return HttpResponseRedirect('/')

def logout(request):
    del request.session['user_id']
    del request.session['fname']
    return HttpResponseRedirect('/')
 from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from import *
from django.template import RequestContext
from django.shortcuts import render_to_response

def add_todo(request):
    if request.method=='POST':
        return HttpResponseRedirect('/')
        return render_to_response('add_todo.html',data)

def edit_todo(request,todo_id):
    if request.method=='POST':
        return HttpResponseRedirect('/')
        return render_to_response('edit_todo.html',data)

def delete_todo(request,todo_id):
    return HttpResponseRedirect('/')

now create a file file in app folder and copy bellow content:
 from django import forms
import re
from django.core.exceptions import ObjectDoesNotExist
from django.forms import ModelForm
from import *

class SignupForm(ModelForm):
    class Meta:

now create templates folder in root folder where have app and data folder
 and create bellow 5 html file:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<title>Todo list | {% block title %}{% endblock %}</title>
margin:10px 0;
border-right:1px solid #000;
table tr:first-child td{
    border-bottom:1px solid #000;
    border-top:1px solid #000;
{% block head %}{% endblock %}
{% block header %}{% endblock %}
{% block content %}{% endblock %}
<div class="footer">
{% block footer %}{% endblock %}
copyright 2014

 {%extends "base.html" %}
{%block title %}{{fname}}{% endblock %}
{%block content %}
<div class="mid">
<div><img src="./images/profile/thumb/{{pic}}" width="50" style="vertical-align:middle;">&nbsp;<b>{{fname}}</b>&nbsp;|&nbsp;<a href="/logout">Logout</a></div>
<h1>Todo List</h1>
<a href="add-todo">Add Todo</a>
<table width="100%" cellpadding="5">
<td width="20%">Date</td><td width="70%">Job</td><td width="5%">&nbsp;</td>
{%for row in todo%}
<tr><td>{{row.created_date|date:"D d M Y"}}</td><td>{{row.todo_job}}</td><td><a href="/edit-todo/{{}}">Edit</a>&nbsp;|&nbsp;<a href="/delete-todo/{{}}">Delete</a></td></tr>
{% endblock %}

{%extends "base.html" %}
{%block title %}Welcome{% endblock %}
{%block content %}
<div class="mid">
<div class="left small-div">
<form action="signup" method="post" enctype="multipart/form-data" id="frmId">
<input type="submit" value="signup" class="btn">
<div class="right small-div">
<form action="login" method="post">
<input type="text" name="email" required>
<input type="password" name="password" required>
<input type="submit" value="Login">
<div style="clear:both;"></div>

{% endblock %}

{%extends "base.html"%}
{%block title %}Add | {{fname}}{% endblock %}
{%block content%}
<div class="mid">
<div><b>{{fname}}</b>&nbsp;|&nbsp;<a href="/logout">Logout</a></div>
<h1>Add Todo List</h1>
<form action="add-todo" method="post">
<textarea name="job" required></textarea>
<input type="submit" value="Add">
{% endblock %}
{%extends "base.html"%}
{%block title %}Add | {{fname}}{% endblock %}
{%block content%}
<div class="mid">
<div><b>{{fname}}</b>&nbsp;|&nbsp;<a href="/logout">Logout</a></div>
<h1>Add Todo List</h1>
<form action="" method="post">
<textarea name="job" required>{{todo.todo_job}}</textarea>
<input type="hidden" name="id" value="{{}}">
<input type="submit" value="Add">
{% endblock %}

now edit file in store folder and copy bellow content:
from django.conf.urls import patterns, include, url
from import home
from import todo
from django.contrib import admin
import os.path


site_media = os.path.join(os.path.dirname(__file__), 'site_media')

urlpatterns = patterns('',url(r'^admin/', include(,)
urlpatterns += patterns('',(r'^$', home.main_page),)
urlpatterns += patterns('',(r'^signup$', home.signup),)
urlpatterns += patterns('',(r'^login$', home.login),)
urlpatterns += patterns('',(r'^logout$', home.logout),)
urlpatterns += patterns('',(r'^add-todo$', todo.add_todo),)
urlpatterns += patterns('',(r'^edit-todo/(?P<todo_id>\d+)$', todo.edit_todo),)
urlpatterns += patterns('',(r'^delete-todo/(?P<todo_id>\d+)$', todo.delete_todo),)

 now your application have completed now start server type bellow command from store folder where folder have

$python runserver

then open localhost:8000 then you can see bellow screen

so now enjoy your new cocktail .
if you get any problem or anything else you comment without any hesitation