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 import HttpResponse # Create your views here. def index(request): html="<h1 style='color:red'>hello world</h1>" return HttpResponse(html) def web(request): html="<h1>Djang Web</h1>" return HttpResponse(html)
5.在book目录下新建urls.py,目的是将views映射到路径中,
from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path('', views.index), path('web/', views.web), ]
6.在newwebsite的urls.py中映射app的路径
from django.contrib import admin from django.urls import path,include from book import urls urlpatterns = [ path('admin/', admin.site.urls), path('',include(urls)), ]
7.我们就可以通过路径访问到book中的views中的函数了,启动服务器:
python manage.py runserver
在浏览器输入http://127.0.0.1:8000/web/
输入http://127.0.0.1:8000/
具体路线如下:
地址中的空格会被省略掉。