创建应用:以helloworld目录为例
1.进入manage.py的同级目录
2.键入命令python manage.py startapp blog //blog为app的名字,随意取
3.添加应用名到helloworld的settings.py的INSTALLED_APPS中
4.在blog中的views.py中编写python代码
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
5.配置url,编辑HelloWorld的urls.py文件
url函数中,第一个参数未url本身,第二个为响应函数,第三个为url名称
"""helloworld URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
import blog.views as bv
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', bv.index),
]
5.启动server, python manage.py runserver
通过http://127.0.0.1:8000/index/访问