实现了下,没动前端的东西。实现功能的顺序:view, finish, back, delete, add, update. 逻辑比较简单,正则表达式的部分还有待熟悉。做的过程中写完view小激动就忘记配置url了。前端可以先注释掉还没有实现的功能,然后再一一补上就可以了。
项目下的urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^todo/', include('todo.urls')),
url(r'^admin/', include(admin.site.urls)),
]
app下的urls.py:
from django.conf.urls import patterns,url
from todo import views
#url --> view (handle)--> webpage
urlpatterns = patterns(
'', #have to add this line why
url(r'^$',views.todolist,name='todo'),
url(r'^finish/(?P\d+)/$',views.todofinish,name='finish'),
url(r'^back/(?P\d+)/$',views.todoback,name='back'),
url(r'^delete/(?P\d+)/$',views.tododelete,name='delete'),
url(r'^add/$',views.todoadd,name='add'),
url(r'^update/(?P\d+)/$',views.todoupdate,name='update'),
)
views部分:
from django.shortcuts import render
from django.http import HttpResponseRedirect,Http404
from models import Todo
from django.contrib.auth.models import User
def todolist(request):
todolist = Todo.objects.filter(flag=1).order_by('-pubtime')
finishlist= Todo.objects.filter(flag=0).order_by('-pubtime')
context = {'todolist':todolist,'finishlist':finishlist} #curly bracket for dictionary
return render(request,'simpleTodo.html',context)
def todofinish(request,id=''):#give the todo id
todo = Todo.objects.get(id=id)
if(todo.flag=='1'):
todo.flag='0' #0 is finish
todo.save()
#no need, becaue the redirection will ask view.todolist
#todolist = Todo.objects.filter(flag=1)
#todofinish = Todo.objects.filter(flag=0)
#context = {'todolist':todolist}
return HttpResponseRedirect('/todo/')
todolist = Todo.objects.filter(flag=1)
todofinish = Todo.objects.filter(flag=0)
context = {'todolist':todolist,'todofinish':todofinish}
return render(request,'simpleTodo.html',context)
def todoback(request,id=''):
todo = Todo.objects.get(id=id)
if todo.flag=='0':
todo.flag='1'
todo.save()
return HttpResponseRedirect('/todo/')
def tododelete(request,id=''):
try:
todo = Todo.objects.get(id=id)
except Exception:
raise Http404
#if element exists
if todo:
todo.delete()
return HttpResponseRedirect('/todo/')
def todoadd(request):
if request.method=='POST':
todocontent = request.POST['todo']
priority = request.POST['priority']
user = User.objects.get(id='1')
todo = Todo(user=user, todo=todocontent, priority=priority, flag='1')
todo.save()
#return HttpResponseRedirect('/todo/')
todolist = Todo.objects.filter(flag=1)
todofinish = Todo.objects.filter(flag=0)
context = {'todolist':todolist,'todofinish':todofinish}
#return render(request,'simpleTodo.html',context)
print "go this way"
return HttpResponseRedirect('/todo/')
else: print "wrong"
def todoupdate(request,id=''):
if request.method == 'POST':
try:
todo = Todo.objects.get(id=id)
except Exception:
return HttpResponseRedirect('/todos/')
todocontent = request.POST['todo']
priority = request.POST['priority']
todo.todo = todocontent
todo.priority = priority
todo.save()
return HttpResponseRedirect('/todo/')
else:
try:
todo = Todo.objects.get(id=id)
except Exception:
raise Http404
return render(request,'updatatodo.html',{'todo':todo})
model:
from django.db import models
from django.contrib.auth.models import User
class Todo(models.Model):
user = models.ForeignKey(User)
todo = models.CharField(max_length=50)
flag = models.CharField(max_length=2)
priority = models.CharField(max_length=2)
pubtime = models.DateTimeField(auto_now_add=True)
#difference between auto now auto now add ;auto now cannot modigy
def __str__(self): # __unicode__ on Python 2
return self.todo
感觉要学的内容还很多,可能需要看大量的文档才可以做多一点的功能。
参考源代码:
https://github.com/zongxiao/Django-Simple-Todo