整个网站结构
|-- myweb | |-- __init__.py | |-- apgi.py | |-- settings.py | |-- urls.py | |-- views.py | |-- wsgi.py |-- manage.py `-- templates `-- hello.html
/myweb/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", ], }, }, ]
/myweb/urls.py 文件部分代码:
urlpatterns = [ path("admin/", admin.site.urls), path('test', views.hello), ]
/myweb/viwes.py 文件代码:
from django.shortcuts import render def hello(request): context = {} context['hello'] = '罗分明!' context['user'] = request.GET.get('user', '') context['pwd'] = request.GET.get('pwd', '') return render(request, 'hello.html', context)
/myweb/templates/hello.html 文件代码:
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>hello {{hello}}</h1> <h1>用户名: {{user}}</h1> <h1>密码: {{pwd}}</h1> </body> </html>
启动网站(用控制台启动 Win+R cmd)进入网站根目录
python manage.py runserver 0.0.0.0:80
浏览器打开 http://192.168.1.23/test?user=lqwvje&pwd=123456 的效果
整个项目源码:https://pan.baidu.com/s/1DTd6fydA7UpbL1foLLxPjw?pwd=verw
相关文章,Python Django创建一个简单的web网站: http://www.luofenming.com/show.aspx?id=2a32c75cd28e4a3c95e7d738357e62eb
本文来自 www.luofenming.com