python Django 创建应用过程图示详解
作者:Blue·Sky 时间:2022-10-29 03:04:40
如图输入如下命令
python manage.py startapp apitest
添加应用到 autotest项目项目下
在settings.pyo 中加入“apitest”,如下图
创建视图
在apitest/views中添加test 函数
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def test(request):
return HttpResponse('hello test')
创建映射
现在把视图中的函数映射到浏览器前端页面,在autotest/urls.py 中加入如下内容:
from django.contrib import admin
from django.urls import path
from apitest import views # 导入 views ,注意项目路径,打开时要 从autotest目录打开。
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',views.test), # 添加 路径及函数
]
启动服务:
python manage.py runserver
若此时访问 http://127.0.0.1:8000/
会提示如下错误
根据提示可以到指定setting 文件下修改配置
修改后又提示如图错误,提示为:应该 设置 ALLOWED_HOSTS = [] 因为我们的服务地址是 127.0.0.1 。所以设置为该值,如果输入其他值,访问127.0.0.1,则无法访问。如下图。
再次执行即不再报错。
再次访问时提示
此时可以加上访问路径,如下操作。
浏览器访问:http://127.0.0.1:8000/test/
创建模版
1、在apitest 下创建templates 文件夹,在该文件夹下创建login.html文件
并添加如下内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="test/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<h1>login</h1>
<form method="post" action="login/">
{% csrf_token %}
<br> <input name="username" type="text" placeholder="username">
<br> <input name="password" type="password" placeholder="password">
{{ error }} <br>
<br> <button id="submit" type="submit">submit</button>
</form>
</body>
</html>
2、在autotest/urls.py 中创建映射
from django.contrib import admin
from django.urls import path
from apitest import views # 导入 views ,注意项目路径,打开时要 从autotest目录打开。
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',views.test), # 添加 路径及映射函数
path('login/',views.login), # 这里添加这个路径,当访问login/ 时 会映射到views里的login函数
]
3、在apitest/views.py 中创建login函数
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def test(request):
return HttpResponse('hello test')
def login(request):
return render(request,'login.html') # 这里返回 使用了render 返回指定页面内容。
4、访问登录页:http://127.0.0.1:8000/login/ 页面展示如下:
来源:https://www.cnblogs.com/BlueSkyyj/p/9346242.html
标签:python,django,创建,应用


猜你喜欢
ASP获取ACCESS数据库的表名以及表名对应的字段名和字段类型
2009-12-25 18:57:00
微软建议的ASP性能优化28条守则(3)
2008-02-24 16:30:00
手把手教你pycharm专业版安装破解教程(linux版)
2023-03-03 08:58:56

使用 OpenCV-Python 识别答题卡判卷功能
2023-02-03 07:01:43

jQuery Ajax全解析
2024-04-09 19:45:00
python 获取毫秒级时间问题的解决
2022-01-06 15:27:30
在Keras中CNN联合LSTM进行分类实例
2023-10-12 22:07:07
vue-cli4.5.x快速搭建项目
2024-04-27 15:52:18

python-itchat 获取微信群用户信息的实例
2022-11-09 18:15:41
python request要求接口参数必须是json数据的处理方式
2023-07-14 03:12:18
javascript实现des解密加密全过程
2023-07-02 05:18:50
七种Python代码审查工具推荐
2021-06-16 07:07:34
CodeIgniter连贯操作的底层原理分析
2024-05-02 17:15:11
解决golang 关于全局变量的坑
2024-02-17 05:46:02
全兼容可高亮二级缓冲折叠菜单
2010-06-03 16:53:00

JS实现页面滚动到关闭时的位置与不滚动效果
2024-04-10 10:47:56
Oracle实现动态SQL的拼装要领
2024-01-15 07:38:23
Python+matplotlib实现简单曲线的绘制
2023-01-05 17:21:27

用途相似的标签
2008-05-23 13:11:00
Linux系统彻底删除Mysql的详细教程
2024-01-22 00:36:22
