Skip to content

Latest commit

 

History

History
63 lines (59 loc) · 1.2 KB

应用与路由.md

File metadata and controls

63 lines (59 loc) · 1.2 KB
  1. python manage.py startapp blog
  2. tree /F
│  db.sqlite3
│  manage.py
├─blog
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py│
│  │  views.py
│  │  __init__.py
│  │
│  ├─migrations
│     │─__init__.py
│
├─DjangoBlog
│  │  settings.py
│  │  urls.py
│  │  wsgi.py
│  │─ __init__.py
  1. DjangoBlog/settings.py
INSTALLED_APPS = [
    'blog.apps.BlogConfig',  #添加blog应用
    ...
]
  1. blog/views.py
from django.shortcuts import render
form django.http import HttpResponse

def home(request):
    return HttpResponse('<h1>blog home</h1>')

def about(request):
    return HttpResponse('<h1>blog about</h1>')
  1. blog/urls.py
from django.urls import path
from . import views

urlpatterns = [   
    path('', views.home, name='blog-home'),
    path('about/', views.about, name='blog-about')
] 
  1. DjangoBlog/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [   
    path('admin/', admin.site.urls),
    Path('', .include('blog.urls')),
]
  1. python manage.py runserver
  2. https://localhost:8000/https://localhost:8000/about