在页面上显示内容可以简单的用django.http.HttpResponse来显示我们需要的内容,但是当我们需要一些处理一些复杂的数据或者从数据库读出来的操作要显示在html标签内或者js代码中就需要用渲染模板的方法。
首先,使用一下代码创建一个名为ymxz的工程(读者可以根据需要设置自己的工程名):
django-admin startproject ymxz
创建工程之后,cd到工程目录,创建一个名为myapp的应用
python manage.py startapp myapp
在myapp目录下创建一个名为templates的目录,在这个目录下面创建一个网页,我这里叫index.html
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'index.html')
from django.conf.urls import patterns, include, url
from django.contrib import admin
from myapp import views
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ymxz.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^index/', views.index),
)
修改INSTALLED_APPS这个字段添加我们的app–myapp
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
在cmd中执行以下语句
python manage.py runserver
from django.shortcuts import render
# Create your views here.
def index(request):
str = 'hello world django'
return render(request, 'index.html', {'test': str})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ test }}
</body>
</html>
注:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有