当前位置: 移动技术网 > IT编程>脚本编程>Python > Django学习笔记1

Django学习笔记1

2018年05月05日  | 移动技术网IT编程  | 我要评论

大蛇王txt,华北科技学院是几本,土鸡蛋价格

重点在注释#

1.views.py

 1 from django.shortcuts import render
 2 from django.http import *
 3 #from django.template import RequestContext,loader
 4 
 5 # Create your views here.
 6 
 7 def index(request):#request是已经封装好的,包括请求头
 8     # temp=loader.get_template('booktest/test.html')#loader表示加载,得到某模板
 9     # return HttpResponse(temp.render())#将模板渲染
10     return  render(request,'booktest/test.html',)#是上面两行代码的封装,简单些,
11     #'booktest/test.html'是templates下的路径,但我们还需要templates的路径,需要到setting.py下配置

2.setting.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#我猜是获得book_hero的路径

 

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],#路径拼接,BASE_DIR在第16行,后面加上templates,
        # C:\Users\OOK\Desktop\book_hero\templates 如果templates放在app里,则这里不用配置
        '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',
            ],
        },
    },
]

 3.views.py里面调用models

from django.shortcuts import render
from django.http import *
#from django.template import RequestContext,loader

#.表示当前目录
#from . import models            #用这种方式需要models.BookInfo这样去调用models里面的类
from .models import *           #用这种方式简单些,可以直接写models里面的类

# Create your views here.

def index(request):#request已经封装好了,包括请求头
    # temp=loader.get_template('booktest/test.html')
    # return HttpResponse(temp.render())

    #bookList=models.BookInfo.objects.all()
    bookList=BookInfo.objects.all()

    content={'list':bookList}

    return  render(request,'booktest/test.html',content)#是上面两行代码的封装,简单些,
    #'booktest/test.html'是templates下的路径,但我们还需要templates的路径,需要到setting.py下配置

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网