其他分享
首页 > 其他分享> > 模板 templates

模板 templates

作者:互联网

setting.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',
            ],
        },
    },
]

templates:

项目目录下添加templates目录

此目录下写HTML

url:

from django.conf.urls import url
from myApp.views import save


urlpatterns = [
    url(r'^index/$', save.index),
]

views:

from django.shortcuts import render
from django.http import HttpResponse
from myApp import modelsimport os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'


def index(request):
    if request.method == "GET":
        return render(request, "index.html")
    else:
        models.UserInfor.objects.create(
            name=name,
            age=age,
            location=location,
            wen=wen
        )
        info_list = models.UserInfor.objects.all()

        return render(request, "index.html", {"info_list": info_list})

        # return HttpResponse("提交成功")

 

标签:templates,index,processors,request,django,context,import,模板
来源: https://www.cnblogs.com/yu121/p/14958936.html