#coding:utf-8
#view
from django.shortcuts import render from django.http import HttpResponse from app1.models import Person from django.http import JsonResponse from django.http import HttpResponseRedirect #跳转 from django.db import connection #执行自定义db import include_test from .forms import AddForm # Create your views here. def index(request): title = u"标题"; info_dict = [{'site': u'名称1', 'content': u'T技术教程'},{'site': u'name2', 'content': u'技术教程2'}] b=Person.objects.all(); return render(request, 'app1/home.htm',{'info_dict': info_dict,"title":title,"persion":b}) def add(request): a = request.GET['a']; b = request.GET['b']; c = int(a) + int(b); return HttpResponse(str(c)); def add2(request, a, b): c = int(a) + int(b); return HttpResponse(str(c)); #below is ajax test def ajax_list(request): a = range(100) return JsonResponse(a, safe=False) def ajax_dict(request): name_dict = {'twz': 'Love python and Django', 'zqxt': 'I am teaching Django'} return JsonResponse(name_dict) def dictfetchall(cursor): #"将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall() ] # 引入我们创建的表单类 def index_from(request): rs=[]; cursor = connection.cursor() if request.method == 'POST':# 当提交表单时 a=request.POST['a'] b= request.POST['b'] #wirte to db sql="INSERT INTO `app1_person` ( `name`, `age`) VALUES ('"+a+"', '"+b+"')"; print sql; #控制台输出 cursor.execute(sql); cursor.execute("SELECT * FROM app1_person order by id desc"); #rs = cursor.fetchall() rs = dictfetchall(cursor) print(rs); return HttpResponseRedirect('/?from=1') #跳转到index界面 #return HttpResponse(str(int(a) + int(b))) else:# 当正常访问时 cursor.execute("SELECT * FROM app1_person order by id desc"); rs = dictfetchall(cursor) #rs = cursor.fetchall() #返回的是元组 return render(request, 'app1/form.htm', {'rs':rs})
#coding:utf-8 """helloworld URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.8/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import include, url from django.contrib import admin from app1 import views as app1_views # new urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^$', app1_views.index, name='home'), # new url(r'^add/$', app1_views.add, name='add'), #正则表达式中 d 代表一个数字,+ 代表一个或多个前面的字符,写在一起 d+ 就是一个或多个数字,用括号括起来的意思是保存为一个子组 url(r'^add2/(d+)/(d+)/$', app1_views.add2, name='add2'), url(r'^add3/(d+)/(d+)/$', app1_views.add2, name='add3'), #ajax url(r'^ajax_list/$', app1_views.ajax_list, name='ajax-list'), url(r'^ajax_dict/$', app1_views.ajax_dict, name='ajax-dict'), #form url(r'^index_from/$', app1_views.index_from, name='index_from'), ]
#coding:utf-8 from django.db import models #from __future__ import unicode_literals #from django.utils.encoding import python_2_unicode_compatible # Create your models here. import os os.environ['DJANGO_SETTINGS_MODULE'] = 'helloworld.settings' #DJANGO_SETTINGS_MODULE 环境配置 class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField() def __unicode__ (self): #用于后台显示出来操作主要看admin.py return self.name class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __unicode__ (self): return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __unicode__ (self): return self.name class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() n_pingbacks = models.IntegerField() rating = models.IntegerField() def __unicode__ (self): return self.blog
1. 新建一个 django project
1
|
django-admin.py startproject project-name |
一个 project 为一个项目,project-name 项目名称,改成你自己的,要符合Python 的变量命名规则(以下划线或字母开头)
2. 新建 app
1
2
|
python manage.py startapp app-name 或 django-admin.py startapp app-name |
一般一个项目有多个app, 当然通用的app也可以在多个项目中使用。
3. 同步数据库
1
2
3
4
5
|
python manage.py syncdb 注意:Django 1.7.1及以上的版本需要用以下命令 python manage.py makemigrations python manage.py migrate |