Django创建项目tongfunet创建应用hello_tongfu

鬼谷子叔叔 - 2024-09-20 11:35

创建项目tongfunet

创建项目tongfunet。

django-admin startproject tongfunet

创建应用hello_tongfu

进入项目tongfunet目录。

cd tongfunet

创建应用hello_tongfu。

python -u manage.py startapp hello_tongfu

实现hello_tongfu应用

编辑tongfunet/settings.py,在INSTALLED_APPS里添加hello_tongfu应用。

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'hello_tongfu',
]

编辑hello_tongfu/views.py,创建index函数。

# 引入HttpResponse
from django.shortcuts import render, HttpResponse

...

# 实现index函数
def index(r):
    return HttpResponse("hello tongfu")

编辑tongfunet/urls.py,添加hello_tongfu应用的index函数的映射。

# 引入hello_tongfu视图
from hello_tongfu import views as hello_tongfu_views

...

# 映射hello_tongfu_views.index函数
urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello_tongfu/', hello_tongfu_views.index),
]

看效果

启动项目tongfunet

python -u manage.py runserver

在浏览器里输入网址http://127.0.0.1:8000/hello_tongfu/查看效果。