刚刚看完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<id>\d+)/$', views.todofinish, name='finish'), url(r'^todobackout/(?P<id>\d+)/$', views.todoback, name='backout'), url(r'^updatetodo/(?P<id>\d+)/$', views.updatetodo, name='update'), url(r'^tododelete/(?P<id>\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
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <meta name="author" content=""> <link rel="shortcut icon" href="/static/ico/favicon.png"> <title>Starter Template for Bootstrap</title> <!-- Bootstrap core CSS --> <link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/static/css/starter-template.css" rel="stylesheet"> <!-- Just for debugging purposes. Don't actually copy this line! --> <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]--> <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.min.js"></script> <script src="http://cdn.bootcss.com/respond.js/1.3.0/respond.min.js"></script> <![endif]--> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Project name</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="/">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#contact">Contact</a></li> </ul> </div><!--/.nav-collapse --> </div> </div> <div class="container"> {% block body %}{% endblock %} </div><!-- /.container --> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="http://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> <script src="/static/bootstrap/js/bootstrap.min.js"></script> </body> </html>
simpleTodo.html
{% extends "base.html" %} {% block body %} <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="starter-template"> <h2 class="text-success"><b>待办事项:</b></h2> <table class="table"> {% for todo in todolist %} {% if todo.priority == '1' %} <tr class='danger'> {% endif %} {% if todo.priority == '2' %} <tr class='warning'> {% endif %} {% if todo.priority == '3' %} <tr class='success'> {% endif %} <td>{{ todo.id }}</td> <td>{{ todo.todo }}</td> <td> <a href="{% url "finish" todo.id %}"><span class="glyphicon glyphicon-ok"></span></a> <a href="{% url "update" todo.id %}"><span class="glyphicon glyphicon-edit"></span></a> <a href="{% url "delete" todo.id %}"><span class="glyphicon glyphicon-trash"></span></a> </td> </tr> {% endfor %} </table> <a href="{% url "add" %}" class="btn btn-success active">ADD</a> </div> </div> </div> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="starter-template"> <h2 class="text-success"><b>已办事项:</b></h2> <table class="table"> {% for todo in finishtodos %} {% if todo.priority == '1' %} <tr class='danger'> {% endif %} {% if todo.priority == '2' %} <tr class='warning'> {% endif %} {% if todo.priority == '3' %} <tr class='success'> {% endif %} <td>{{ todo.id }}</td> <td><i><S>{{ todo.todo }}</S></i></td> <td> <a href="{% url "backout" todo.id %}"><span class="glyphicon glyphicon-repeat"></span></a> <a href="{% url "update" todo.id %}"><span class="glyphicon glyphicon-edit"></span></a> <a href="{% url "delete" todo.id %}"><span class="glyphicon glyphicon-trash"></span></a> </td> </tr> {% endfor %} </table> </div> </div> </div> {% endblock %}
addtodo.html
{% extends "base.html" %} {% block body %} <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">ADD Todo:</h3> </div> <div class="panel-body"> <form class="form-horizontal" role="form" method="post">{% csrf_token %} <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">任务内容:</label> <div class="col-sm-10"> <textarea class="form-control" rows="3" name="todo">{{ todo.todo }}</textarea> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">任务等级:</label> <div class="col-sm-10"> <label class="checkbox-inline"> <input type="radio" name="priority" value="1" {% if not todo.priority or todo.priority == '1' %} checked {% endif %}><span class="btn btn-danger btn-xs">重要</span> </label> <label class="checkbox-inline"> <input type="radio" name="priority" value="2" {% if todo.priority == '2' %} checked {% endif %}><span class="btn btn-warning btn-xs">警告</span> </label> <label class="checkbox-inline"> <input type="radio" name="priority" value="3" {% if todo.priority == '3' %} checked {% endif %}><span class="btn btn-success btn-xs">成功</span> </label> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">提交</button> </div> </div> </form> </div> </div> </div> </div> {% endblock %}
最后效果如下:
参考文章:http://www.cnblogs.com/cacique/archive/2012/10/06/2713415.html