1.创建django项目:
命令:django-admin.py startproject todoManager
2.在此项目下创建apps应用
命令:python manage.py startapp apps
目录如下:
3.打开todoManager/todoManager/settings.py文件,将应用添加进去:
4.打开todoManager/todoManager/settings.py文件,设置连接数据库,以下连接的是MYSQL
5.打开todoManager/apps/models.py文件,添加如下内容,创建数据库,创建User表,用户名和密码两个字段。
6.进行数据库的同步:
查看django版本:输入python ,enter;import django django.get_version()
我的版本是1.9之前的:在Django1.9之前,数据库同步只需要一条命令,那就是python manage.py syncdb
在1.9之后,python manage.py makemigrations;python manage.py migrate。
修改原有app模块中的module中的属性。
1、rm -rf ./ops/migrations #先进入所在app模块中,删除 migrations 目录
2、mkdir ./ops/migrations #创建migrations
3、python manage.py makemigrations ops #更新migrations 中对module的记录
4、python manage.py migrate #同步数据库
后生成的 apps_user 表就是我们models.py 中所创建的User类。
如果没有提示生成超级用户:
python manage.py createsuperuser
7. 配置URL
7.1 打开todoManager/todoManager/urls.py
7.2 在todoManager/apps/目录下创建urls.py文件:
from django.conf.urls import patterns, url,include
from django.contrib import admin
from apps import views
urlpatterns = patterns('',
url(r'^regist/$',views.regist,name = 'regist'),
url(r'^login/$',views.login,name = 'login'),
url(r'^index/$',views.index,name = 'index'),
url(r'^logout/$',views.logout,name = 'logout'),
)
7.3 创建视图
打开todoManager/apps/views.py 文件:
#-*- coding: utf-8 -*-
from django.shortcuts importrender#Create your views here.#coding=utf-8
from django.shortcuts importrender,render_to_responsefrom django.http importHttpResponse,HttpResponseRedirectfrom django.template importRequestContextfrom django importformsfrom apps.models importUserfrom pymongo importMongoClientfrom django.views.decorators.csrf importcsrf_protect, csrf_exempt
mongo_client= MongoClient('192.168.86.126', 27017)#表单
classUserForm(forms.Form):
username= forms.CharField(label=u'用户名',max_length=100)
password= forms.CharField(label=u'密 码',widget=forms.PasswordInput())#注册#@csrf_protect
defregist(req):if req.method == 'POST':
uf=UserForm(req.POST)ifuf.is_valid():#获得表单数据
username = uf.cleaned_data['username']
password= uf.cleaned_data['password']#添加到数据库
User.objects.create(username= username,password=password)return HttpResponse('regist success!!')else:
uf=UserForm()return render_to_response('regist.html',{'uf':uf}, context_instance=RequestContext(req))#登陆#@csrf_protect
deflogin(req):
username= req.COOKIES.get('username','')if req.method == 'POST':
uf=UserForm(req.POST)ifuf.is_valid():#获取表单用户密码
username = uf.cleaned_data['username']
password= uf.cleaned_data['password']#获取的表单数据与数据库进行比较
user = User.objects.filter(username__exact = username,password__exact =password)ifuser:#比较成功,跳转index
response = HttpResponseRedirect('/apps/index/')#将username写入浏览器cookie,失效时间为3600
response.set_cookie('username',username,3600)returnresponseelse:#比较失败,还在login
return HttpResponseRedirect('/apps/login/')else:
uf=UserForm()return render_to_response('login.html',{'uf':uf}, context_instance=RequestContext(req) )#登陆成功#@csrf_protect
defindex(req):
username= req.COOKIES.get('username','')ifusername:
key_words_coll= mongo_client['yidong_zixun']['key_words']
categorys={}
tasks={}
results= key_words_coll.find().batch_size(30)for result inresults:
category_id= result["category_id"]
category_name= result["category_name"]
_id= result["_id"]
name= result["name"]if notcategorys.has_key(category_id):
categorys[category_id]=category_nameif nottasks.has_key(category_id):
tasks[category_id]=[]
tasks[category_id].append({"task_id":_id,"name":name})else:
tasks[category_id].append({"task_id":_id,"name":name})return render_to_response('index.html' ,{'username':username,'categorys':categorys,"tasks":tasks}, context_instance=RequestContext(req))else:return HttpResponseRedirect('/apps/login/')#退出#@csrf_protect
deflogout(req):#response = HttpResponse('logout !!')
response = HttpResponseRedirect('/apps/login/')
response.delete_cookie('username')return response
View Code
7.4 创建模板
先在todoManager/apps/目录下创建templates目录,接着在todoManager/apps/templates/目录下创建base.html、regist.html、login.html 、index.html文件:
7.5设置模板路径
打开todoManager/todoManager/settings.py文件,在底部添加:
8.运行django
命令:python manage.py runserver 0.0.0.0:8000
9.迁移功能非常强大,可以让你在开发过程中不断修改你的模型而不用删除数据库或者表然后再重新生成一个新的 —— 它专注于升级你的数据库且不丢失数据。
修改你的模型(在models.py文件中)。