刚刚看完django book,网上找了些例子来练习最基本的操作:
python2.7.3
django1.6.2
mysql5.5
前端套用bootstrap
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Todo(models.Model):
user = models.ForeignKey(User)
todo = models.CharField(max_length=50)
flag = models.CharField(max_length=2, default='1')
priority = models.CharField(max_length=2, default='0')
pubtime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return u'%d %s %s' % (self.id, self.todo, self.flag)
class Meta:
ordering = ['priority', 'pubtime']
priority 为优先级 排序时优先级高的在前面,越小优先级越高;然后再按时间排序,越早越靠前
flag 标记是否完成 默认为1表示未完成
admin.py
from django.contrib import admin
# Register your models here.
from todoApp.models import Todo
class TodoAdmin(admin.ModelAdmin):
list_display = ('user', 'todo', 'priority', 'flag', 'pubtime')
list_filter = ('pubtime',)
ordering = ('-pubtime',)
admin.site.register(Todo, TodoAdmin)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from todoApp import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'simpleTodo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.todolist, name='todo'),
url(r'^addtodo/$', views.addtodo, name='add'),
url(r'^todofinish/(?P\d+)/$', views.todofinish, name='finish'),
url(r'^todobackout/(?P\d+)/$', views.todoback, name='backout'),
url(r'^updatetodo/(?P\d+)/$', views.updatetodo, name='update'),
url(r'^tododelete/(?P\d+)/$', views.tododelete, name='delete'),
)
views.py
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.http import Http404
from todoApp.models import Todo
from django.contrib.auth.models import User
# Create your views here.
def todolist(request):
todolist = Todo.objects.filter(flag=1)
finishtodos = Todo.objects.filter(flag=0)
return render_to_response('simpleTodo.html',
{'todolist':todolist, 'finishtodos':finishtodos},
context_instance=RequestContext(request))
def todofinish(request, id=""):
todo = Todo.objects.get(id=id)
if todo.flag == "1":
todo.flag = '0'
todo.save()
return HttpResponseRedirect('/')
todolist = Todo.objects.filter(flag=1)
finishtodos = Todo.objects.filter(flag=0)
return render_to_response('simpleTodo.html',
{'todolist':todolist, 'finishtodos':finishtodos},
context_instance=RequestContext(request))
def todoback(request, id=""):
todo = Todo.objects.get(id=id)
if todo.flag == '0':
todo.flag = 1
todo.save()
return HttpResponseRedirect('/')
todolist = Todo.objects.filter(flag=1)
finishtodos = Todo.objects.filter(flag=0)
return render_to_response('simpleTodo.html',
{'todolist':todolist, 'finishtodos':finishtodos},
context_instance=RequestContext(request))
def tododelete(request, id=""):
try:
todo = Todo.objects.get(id=id)
except Exception:
raise Http404
if todo:
todo.delete()
return HttpResponseRedirect('/')
todolist = Todo.objects.filter(flag=1)
finishtodos = Todo.objects.filter(flag=0)
return render_to_response('simpleTodo.html',
{'todolist':todolist, 'finishtodos':finishtodos},
context_instance=RequestContext(request))
def addtodo(request):
if request.method == 'POST':
atodo = request.POST['todo']
priority = request.POST['priority']
user = User.objects.get(id='1')
todo = Todo(user=user, todo=atodo, priority=priority, flag='1')
todo.save()
return HttpResponseRedirect('/')
else:
return render_to_response('addTodo.html',context_instance=RequestContext(request))
def updatetodo(request, id=''):
if request.method == 'POST':
atodo = request.POST['todo']
priority = request.POST['priority']
todo = Todo.objects.filter(id=id).update(todo=atodo,priority=priority)
return HttpResponseRedirect('/')
else:
mytodo = Todo.objects.get(id=id)
return render_to_response("addTodo.html",{'todo':mytodo},context_instance=RequestContext(request))
base.html
Starter Template for Bootstrap
{% block body %}{% endblock %}
simpleTodo.html
{% extends "base.html" %}
{% block body %}
待办事项:
{% for todo in todolist %}
{% if todo.priority == '1' %}
{% endif %}
{% if todo.priority == '2' %}
{% endif %}
{% if todo.priority == '3' %}
{% endif %}
` todo`.`id `
` todo`.`todo `
{% endfor %}
ADD
{% endblock %}
addtodo.html
{% extends "base.html" %}
{% block body %}
ADD Todo:
{% endblock %}
最后效果如下:
参考文章:http://www.cnblogs.com/cacique/archive/2012/10/06/2713415.html