1.新建django项目
django-admin startproject newwebsite
2.建立app
在newwebsite目录下:python manage.py startapp book
3.在settings.py的INSTALLED_APPS中注册'book'
4.在book的view.py中建立两个函数:
from django.shortcuts import render from django.http HttpResponse # Create your views here. def index(request): html="<h1 style='color:red'>hello world</h1>" return HttpResponse(html) web(request): html=<h1>Djang Web</h1>return HttpResponse(html)
5.在book目录下新建urls.py,目的是将views映射到路径中,
from django.contrib admin from django.urls path from . views urlpatterns = [ path('',views.index),path('web/'path,include from book urls urlpatterns =admin/函数了,启动服务器:
python manage.py runserver在浏览器输入http://127.0.0.1:8000/web/
输入http://127.0.0.1:8000/
具体路线如下:
地址中的空格会被省略掉。
原文链接:/django/991017.html