标签:win token style file time tty base 记录 har
1 <link rel="stylesheet" href="/static/css/style.css">
1 TEMPLATES = [ 2 { 3 ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, 4 ‘DIRS‘: [os.path.join(BASE_DIR, ‘templates‘)], 5 ‘APP_DIRS‘: True, 6 ‘OPTIONS‘: { 7 ‘context_processors‘: [ 8 ‘django.template.context_processors.debug‘, 9 ‘django.template.context_processors.request‘, 10 ‘django.contrib.auth.context_processors.auth‘, 11 ‘django.contrib.messages.context_processors.messages‘, 12 ], 13 }, 14 }, 15 ]
1 STATIC_URL = ‘/static/‘ 2 STATICFILES_DIRS = [ 3 os.path.join(BASE_DIR, ‘static‘), 4 ]
1 html准备
整备一个html文件,暂命名:msg_index.html
2 在views中编写函数,返回msg_index.html
1 def getIndex(request): 2 #return msg_index.html page 3 return render(request, ‘msg_index.html‘)
3 urls文件中进行路由配置
1 urlpatterns = [ 2 url(r‘^admin/‘, admin.site.urls), 3 url(r‘^index/‘, getIndex), 4 ]
1 DATABASES = { 2 ‘default‘: { 3 ‘ENGINE‘: ‘django.db.backends.mysql‘, 4 ‘NAME‘: ‘test1‘, 5 ‘USER‘: ‘root‘, 6 ‘PASSWORD‘: ‘111111‘, 7 ‘HOST‘:‘127.0.0.1‘ 8 } 9 }
点击Tools > Run manage.py 提示:Task No module named MySQLdb,表示我们没有安装mysql驱动
1 INSTALLED_APPS = [ 2 ‘django.contrib.admin‘, 3 ‘django.contrib.auth‘, 4 ‘django.contrib.contenttypes‘, 5 ‘django.contrib.sessions‘, 6 ‘django.contrib.messages‘, 7 ‘django.contrib.staticfiles‘, 8 ‘message‘, 9 ]
1 from django.db import models 2 3 class UserMessage(models.Model): 4 name = models.CharField(max_lenth=20, verbose_name=u‘用户名‘) 5 email = models.EmailField(verbose_name=u‘邮箱‘) 6 address = models.CharField(max_length=200, verbose_name=u‘地址‘) 7 message = models.CharField(max_length=500, verbose_name=u‘留言‘) 8 9 class Meta: 10 verbose_name = u‘用户留言信息‘
1 __all__ = [str(x) for x in ( 2 ‘AutoField‘, ‘BLANK_CHOICE_DASH‘, ‘BigIntegerField‘, ‘BinaryField‘, 3 ‘BooleanField‘, ‘CharField‘, ‘CommaSeparatedIntegerField‘, ‘DateField‘, 4 ‘DateTimeField‘, ‘DecimalField‘, ‘DurationField‘, ‘EmailField‘, ‘Empty‘, 5 ‘Field‘, ‘FieldDoesNotExist‘, ‘FilePathField‘, ‘FloatField‘, 6 ‘GenericIPAddressField‘, ‘IPAddressField‘, ‘IntegerField‘, ‘NOT_PROVIDED‘, 7 ‘NullBooleanField‘, ‘PositiveIntegerField‘, ‘PositiveSmallIntegerField‘, 8 ‘SlugField‘, ‘SmallIntegerField‘, ‘TextField‘, ‘TimeField‘, ‘URLField‘, 9 ‘UUIDField‘, 10 )]
>
1 def getIndex(request): 2 #insert 3 if request.method == ‘POST‘: 4 #获取请求值 5 name = request.POST.get(‘name‘, ‘‘) #需要与form中的控件名相同 6 email = request.POST.get(‘email‘, ‘‘) 7 address = request.POST.get(‘address‘, ‘‘) 8 message = request.POST.get(‘message‘, ‘‘) 9 name = request.POST.get(‘name‘, ‘‘) 10 #插入数据库 11 user_msg = UserMessage2() 12 user_msg.msg_id = 5 13 user_msg.name = name 14 user_msg.email = email 15 user_msg.message = message 16 user_msg.address = address 17 user_msg.save() 18 return render(request, ‘msg_index.html‘)
2 查询
1 def getMSG(request): 2 # select all 3 all_msg = UserMessage2.objects.all() 4 for msg in all_msg: 5 print msg.name 6 #select filter 7 filte_msg = UserMessage2.objects.filter(name=‘张‘, address=‘北京‘) 8 for flt in filte_msg: 9 print flt
3 删除
1 def delMSG(request): 2 # delete 3 all_msg = UserMessage2.objects.filter(name=‘张‘, address=‘北京‘) 4 all_msg.delete() #删除所有 5 for msg in all_msg: 6 msg.delete() #删除某个 7 print msg.name
1 views查询数据
1 def getMSG(request): 2 # 数据库取值 3 msg = None 4 all_msg = UserMessage2.objects.filter(name=‘张‘) 5 if all_msg: 6 msg = all_msg[0] 7 return render(request, ‘msg_index.html‘, {‘msg_data‘:msg,}) #回显使用数据
2 html页面回显
<input id="email" type="email" value="{{ msg_data.email }}" name="email" placeholder="请输入邮箱?"/>
3 url别名
html文件中使用别名:<form action="{% url ‘user_msg‘ %}" method="post" class="smart-green">
标签:win token style file time tty base 记录 har
原文地址:http://www.cnblogs.com/lizitest/p/6849027.html