git:https://github.com/huangantai/todolist.git
1振惰、
pip install django=='2.1'
django-admin startproject todolist
django-admin startapp simpletodo
pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
2佳吞、settings.py 和 urls.py 設置
settings.py (注釋csrf中間件)
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'simpletodo',
'xadmin',
'crispy_forms',
'reversion',
]
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
urls.py
from django.urls import path,include
from simpletodo import views
import xadmin
xadmin.autodiscover()
from xadmin.plugins import xversion
xversion.register_models()
urlpatterns = [
path('admin/', xadmin.site.urls),
path(r'', views.todolist),
path(r'simpletodo/',include('simpletodo.urls'))
]
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
3王财、simpletodo/models.py adminx.py urls.py views.py
models.py
from django.db import models
from django.contrib.auth.models import User
Create your models here.
class Todo(models.Model):
id=models.AutoField(primary_key=True)
user = models.ForeignKey(User,on_delete=models.CASCADE)
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)
def __unicode__(self):
return u'%d %s %s' % (self.id, self.todo, self.flag)
class Meta:
ordering = ['priority', 'pubtime']
adminx.py
import xadmin
from simpletodo.models import Todo
Register your models here.
class TodoAdmin(object):
list_display = ['user', 'todo', 'priority', 'flag', 'pubtime']
list_filter = ['pubtime','priority']
ordering = ['-pubtime']
list_editable=['todo','priority','flag']
xadmin.site.register(Todo, TodoAdmin)
[root@centos8 simpletodo]# vi adminx.py
[root@centos8 simpletodo]# cat adminx.py
from xadmin import views
import xadmin
class BaseSetting(object):
"""xadmin的基本配置"""
enable_themes = True # 開啟主題切換功能
use_bootswatch = True
class GlobalSettings(object):
"""xadmin的全局配置"""
site_title = "todolist" # 設置站點標題
site_footer = "-todolist-" # 設置站點的頁腳
menu_style = "accordion" # 設置菜單折疊
xadmin.site.register(views.CommAdminView, GlobalSettings)
xadmin.site.register(views.BaseAdminView, BaseSetting)
from simpletodo.models import Todo
Register your models here.
class TodoAdmin(object):
list_display = ['user', 'todo', 'priority', 'flag', 'pubtime']
list_filter = ['pubtime','priority']
ordering = ['-pubtime']
list_editable=['todo','priority','flag']
xadmin.site.register(Todo, TodoAdmin)
urls.py
!/usr/bin/python
-- coding: utf-8 --
from django.contrib import admin
from django.urls import path,include
from simpletodo import views
urlpatterns = [
path(r'',views.todolist),
path(r'addtodo/', views.addtodo,name='add'),
path(r'todofinish/<int:id>', views.todofinish,name='finish'),
path(r'todobackout/<int:id>', views.todoback,name='backout'),
path(r'updatetodo/<int:id>', views.updatetodo,name='update'),
path(r'tododelete/<int:id>', views.tododelete,name='delete')
]
views.py
from django.shortcuts import render,render_to_response
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.http import Http404
from simpletodo.models import Todo
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})
def todofinish(request,id=''):
todo=Todo.objects.get(id=id)
print(todo.flag,id)
if todo.flag=="1":
todo.flag="0"
todo.save()
return HttpResponseRedirect('/simpletodo/')
def todoback(request,id=''):
todo=Todo.objects.get(id=id)
if todo.flag=="0":
todo.flag="1"
todo.save()
return HttpResponseRedirect('/simpletodo/')
def tododelete(request,id=''):
try:
todo=Todo.objects.get(id=id)
except Exception:
raise Http404
if todo:
todo.delete()
return HttpResponseRedirect('/simpletodo/')
def addtodo(request):
if request.method=='POST':
atodo=request.POST['todo']
priority=request.POST['priority']
if not priority:priority=0
user = User.objects.get(id='1')
todo = Todo(user=user, todo=atodo, priority=priority, flag="1")
todo.save()
todolist = Todo.objects.filter(flag="1")
finishtodos = Todo.objects.filter(flag="0")
return render_to_response('showtodo.html',{'todolist': todolist, 'finishtodos': finishtodos})
else:
todolist = Todo.objects.filter(flag="1")
finishtodos = Todo.objects.filter(flag="0")
return render_to_response('simpletodo.html',{'todolist': todolist, 'finishtodos': finishtodos})
def updatetodo(request,id=''):
if request.method == 'POST':
atodo = request.POST['todo']
priority = request.POST['priority']
user = User.objects.get(id='1')
todo=Todo.objects.get(id=id)
todo.todo=atodo
todo.priority=priority
todo.save()
todolist = Todo.objects.filter(flag="1")
finishtodos = Todo.objects.filter(flag="0")
return render_to_response('simpletodo.html',{'todolist': todolist, 'finishtodos': finishtodos})
else:
try:
todo = Todo.objects.get(id=id)
except Exception:
raise Http404
return render_to_response('updatatodo.html', {'todo': todo})
4、templates/simpletodo.html updatatodo.html showtodo.html
simpletodo.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Simple Todo{% endblock %}</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<script src="/static/js/modernizr.js"></script>
<script src="/static/js/jquery.js"></script>
{% block extra_head %}
<style>
body {padding-top: 40px;}
.ftodo{text-decoration : line-through ; }
textarea{ width: 97%;
padding: 5px;
font-size: 14px;
resize: vertical;}
</style>
<script type="text/javascript">
function sendtwitter(){
.ajax({
type: "POST",
data: ('#todo').replaceWith(html);
('#myModal form')[0].reset();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
(document).ready(function(){
sendtwitter();
})
</script>
{% endblock %}
</head>
<body>
<div class="container">
<div class="row">
<div class="span8 offset2">
<div id="todo" class="well">
{% block todo %}
<table class="table table-hover">
<thead>
<tr>
<td>
<h3 class="text-success"><a href='/simpletodo/'>待辦事項</a></h3>
</td>
</tr>
</thead>
<tbody>
{% for todo in todolist %}
{% if todo.priority == '1' %}
<tr class='error'>
{% endif %}
{% if todo.priority == '2' %}
<tr class='warning'>
{% endif %}
{% if todo.priority == '3' %}
<tr class='info'>
{% endif %}
<td class="todo">{{ todo.todo }}</td>
<td class="todo">{{ todo.pubtime|date:'Y-m-d'}}</td>
<td class="te">
<div class="span2">
<a href="{% url 'finish' todo.id %}" title="finish"><i class=" icon-ok"></i></a>
<a href="{% url 'update' todo.id %}" title="edit"><i class="icon-edit"></i></a>
<a href="{% url 'delete' todo.id %}" title="delete"><i class="icon-trash"></i></a>
</div>
</td>
</tr>
{% endfor %}
{% for ftodo in finishtodos %}
<tr class='success'>
<td class="ftodo muted">{{ ftodo.todo }}</td>
<td class="ftodo muted">{{ ftodo.pubtime|date:'Y-m-d'}}</td>
<td class="te">
<div class="span2">
<a href="{% url 'backout' ftodo.id %}" title="redo"><i class=" icon-repeat"></i></a>
<a href="{% url 'update' ftodo.id %}" title="edit"><i class="icon-edit"></i></a>
<a href="{% url 'delete' ftodo.id %}" title="delete"><i class="icon-trash"></i></a>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<a class="btn btn-success" href="#myModal" role="button" data-toggle="modal">
<i class="icon-plus icon-white"></i><span > ADD</span>
</a>
{% endblock %}
</div>
</div>
</div>
</div>
<div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Add TODO</h3>
</div>
<form action="" method="post">{% csrf_token %}
<div class="modal-body">
<textarea name="todo" class="txtodo" id="txtodo" required="required">{{ todo.todo }}</textarea>
<fieldset>
<label class="radio inline" for="priority">
<span class="label label-info">Priority</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="1"/>
<span class="label label-important">緊急</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="2"/>
<span class="label label-warning">重要</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="3"/>
<span class="label label-success">一般</span>
</label>
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button id="send" class="btn btn-success" type="submit" name="submit" >Save changes</button>
</div>
</form>
</div>
<script src="/static/js/bootstrap.min.js"></script>
</body>
</html>
updatatodo.html
{% extends 'simpletodo.html'%}
{% block title %} update Todo {% endblock %}
{% block todo %}
<form action="{% url 'update' todo.id %}" method="post">{% csrf_token %}
<div class="modal-body">
<textarea name="todo" class="txtodo" id="txtodo" required="required">{{ todo.todo }}</textarea>
<fieldset>
<label class="radio inline" for="priority">
<span class="label label-info">Priority</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="1" {% if todo.priority == '1' %} checked{% endif %}/>
<span class="label label-important">緊急</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="2" {% if todo.priority == '2' %} checked{% endif %}/>
<span class="label label-warning">重要</span>
</label>
<label class="radio inline" for="priority">
<input type="radio" name="priority" value="3" {% if todo.priority == '3' %} checked{% endif %}/>
<span class="label label-success">一般</span>
</label>
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button id="send" class="btn btn-success" type="submit" name="submit" >Save changes</button>
</div>
</form>
{% endblock %}
showtodo.html
<div id="todo" class="well">
{% block todo %}
<table class="table table-hover">
<thead>
<tr>
<td>
<h3 class="text-success">待辦事項</h3>
</td>
</tr>
</thead>
<tbody>
{% for todo in todolist %}
{% if todo.priority == '1' %}
<tr class='error'>
{% endif %}
{% if todo.priority == '2' %}
<tr class='warning'>
{% endif %}
{% if todo.priority == '3' %}
<tr class='info'>
{% endif %}
<td class="todo">{{ todo.todo }}</td>
<td class="te">
<div class="span2">
<a href="{% url 'finish' todo.id %}" title="finish"><i class=" icon-ok"></i></a>
<a href="{% url 'update' todo.id %}" title="edit"><i class="icon-edit"></i></a>
<a href="{% url 'delete' todo.id %}" title="delete"><i class="icon-trash"></i></a>
</div>
</td>
</tr>
{% endfor %}
{% for ftodo in finishtodos %}
<tr class='success'>
<td class="ftodo muted">{{ ftodo.todo }}</td>
<td class="te">
<div class="span2">
<a href="{% url 'backout' ftodo.id %}" title="finish"><i class=" icon-repeat"></i></a>
<a href="{% url 'update' ftodo.id %}" title="edit"><i class="icon-edit"></i></a>
<a href="{% url 'delete' ftodo.id %}" title="delete"><i class="icon-trash"></i></a>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<a class="btn btn-success" href="#myModal" role="button" data-toggle="modal">
<i class="icon-plus icon-white"></i><span > ADD</span>
</a>
{% endblock %}
</div>
5气破、static/css js img
css
bootstrap.min.css
img
glyphicons-halflings.png glyphicons-halflings-white.png
js
ajaxpost.js bootstrap.js bootstrap.min.js jquery.js modernizr.js
/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
6、修改settings.py
STATIC_ROOT = os.path.join(BASE_DIR, '/static')
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static'),]
7餐抢、
python manage.py makemigrations
python manage.py migrate
python runserver 0.0.0.0:9999 &