当前位置:Gxlcms > 数据库问题 > django基础之数据库操作

django基础之数据库操作

时间:2021-07-01 10:21:17 帮助过:19人阅读

from django.shortcuts import render 2 3 # Create your views here. 4 from blog import models 5 from django.shortcuts import HttpResponse 6 def db_handle(request): 7 models.UserInfo.objects.filter(id=1).update(age=18) #找到id=1的数据,将age改为18 8 return HttpResponse(OK)

数据的查询:

为了让查询出来的数据更加直观地显示出来,这里我们将使用Django的模板功能,让查询出来的数据在WEB浏览器中展示出来

在templates目录下新建一个t1.html的文件,内容如下:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>Django操作数据库</title>
 6 </head>
 7 <body>
 8     <table border="1">
 9         <tr>
10             <th>用户名</th>
11             <th>密码</th>
12             <th>年龄</th>
13         </tr>
14         {% for item in li %}
15         <tr>
16             <td>{{ item.username }}</td>
17             <td>{{ item.password }}</td>
18             <td>{{ item.age }}</td>
19         </tr>
20         {% endfor %}
21     </table>
22 </body>
23 </html>

views.py文件查询数据,并指定调用的模板文件,内容如下:

1 from django.shortcuts import render
2 
3 # Create your views here.
4 from blog import models
5 from django.shortcuts import HttpResponse
6 def db_handle(request):
7     user_list_obj = models.UserInfo.objects.all()
8     return render(request,t1.html,{li:user_list_obj})

注意:由于这里是在工程下面的templates目录下建立的模板,而不是在blog应用中创建的模板,上面views.py文件中调用的t1.html模板,运行时会出现找不到t1.html模板的错误,为了能找到mysite/templates下的模板文件,我们还需要在settings.py文件配置模板的路径:

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,
            ],
        },
    },
]

下面就可以在浏览器中查看:

技术分享

引入JS,CSS等静态文件:

在mysite目录下新建一个static目录,将JS,CSS文件都放在此目录下!并在settings.py文件中指定static目录:

1 STATIC_URL = /static/
2 STATICFILES_DIRS = (
3     os.path.join(BASE_DIR,static),
4 )

表单提交数据:

在Django中要使用post方式提交表单,需要在settings.py配置文件中将下面一行的内容给注释掉:

# ‘django.middleware.csrf.CsrfViewMiddleware‘,

提交表单(这里仍然使用了t1.html):

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>Django表单</title>
 6     <link type="text/css" href="/static/base.css" rel="stylesheet" />
 7 </head>
 8 <body>
 9     <table border="1">
10         <tr>
11             <th>用户名</th>
12             <th>密码</th>
13             <th>年龄</th>
14         </tr>
15         {% for item in li %}
16         <tr>
17             <td>{{ item.username }}</td>
18             <td>{{ item.password }}</td>
19             <td>{{ item.age }}</td>
20         </tr>
21         {% endfor %}
22     </table>
23     <form action="/db_handle/" method="post">
24         <p><input name="username" /></p>
25         <p><input name="password" /></p>
26         <p><input name="age" /></p>
27         <p><input type="submit" value="submit" /></p>
28     </form>
29 </body>
30 </html>

写入数据库(views.py):

 1 from django.shortcuts import render
 2 
 3 # Create your views here.
 4 from blog import models
 5 from django.shortcuts import HttpResponse
 6 def db_handle(request):
 7 
 8     if request.method == "POST":
 9         models.UserInfo.objects.create(username=request.POST[username],password=request.POST[password],age=request.POST[age])
10     user_list_obj = models.UserInfo.objects.all()
11     return render(request, t1.html, {li: user_list_obj})

提交数据后,如下图:

技术分享

django基础之数据库操作

标签:man   sys   run   css   配置   into   from   message   com   

人气教程排行