登录 or

创建模板templates,并添加index.html

在tango_with_rango文件夹下,添加templates文件夹,在文件夹下继续创建rango文件夹,在rango文件夹中添加index.html文件
文件内容如下
<!DOCTYPE html>
<html>

<head>
<title>Rango</title>
</head>

<body>
<h1>Rango says...</h1>
hello world! <strong>{{ boldmessage }}</strong><br />
<a href="/rango/about/">About</a><br />
</body>

</html>

 在setting.py中添加模板路径
 
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR], #此处加入
在views.py中添加类
def index(request):

# Construct a dictionary to pass to the template engine as its context.
# Note the key boldmessage is the same as {{ boldmessage }} in the template!
context_dict = {'boldmessage': "I am bold font from the context"}

# Return a rendered response to send to the client.
# We make use of the shortcut function to make our lives easier.
# Note that the first parameter is the template we wish to use.

return render(request, 'rango/index.html', context_dict)
运行python manage.py runserver,成功可在127.0.0.1:8000/rango中查看

0 个评论

要回复文章请先登录注册