Django 包装python算法

 1、创建django项目

django-admin startproject 项目名称

比如: django-admin startproject test_django

2、进入项目文件夹,修改test_django/setting.py文件

# 修改 ALLOWED_HOSTS ,允许任何主机ip访问;否则可以加具体ip。
ALLOWED_HOSTS = ['*']

# 修改 TEMPLATES,创建文件夹“htmls”存放html文件
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['htmls'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# 修改时区
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

# 添加静态文件夹
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'recognition' / 'results'

3、终端运行 python manage.py runserver,出现以下图片代表成功。

4、创建应用

python manage.py startapp 应用名称

比如:python manage.py startapp testapp

5、打开 testapp/views.py 写入需要的函数。view.py文件属于功能视图,存放应用中各个操作函数的。以下为例:

from django.shortcuts import render
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the recognition index.")

def toHTML(request):
    return render(request, 'start.html')

6、创建testapp/urls.py文件,并写入相应的函数映射。

from django.urls import path
from . import views


urlpatterns = [
    path('index', views.index, name='index'),    # http://127.0.0.1:8000/testapp/index
    path('html', views.toHTML, name='toHTML'),   # http://127.0.0.1:8000/testapp/html
]

7、继续修改test_django/setting.py文件

# 修改 INSTALLED_APPS
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'testapp',
]

8、修改test_django/urls.py文件,并写入相应的函数映射

from django.contrib import admin
from django.urls import path, include, re_path
from django.views.static import serve
from cargopile_django import settings

urlpatterns = [
    path('admin/', admin.site.urls),

    # 识别接口
    # path('', include('recognition.urls')),            # 网址为:http://10.193.13.44:8888/index
    path('recognition/', include('recognition.urls')),  # 网址为:http://10.193.13.44:8888/recognition/index
    
    # 图片保存位置,访问图片网址:http://10.193.13.44:8888/recognition/static/test.jpg
    re_path('static/(?P<path>.*)/', serve, {"document_root": settings.STATIC_ROOT}),
]

9、创建cargopile_django/htmls/start.html

<h2>{{ s }}</h2>
<h2>Now: {{ t }}</h2>

10、运行python manage.py runserver 0:8080测试

#############################################################################

urls.py 文件

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/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.contrib import admin
from django.urls import path

from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    # path('网址中端口号后面追加的内容', '文件名.函数名', name='函数名')
]

urlpatterns中,添加程序启动运行的功能文件。path()中,

第一个参数:浏览器网址中,端口号后面追加的内容。

        当为空时,网址为: http://127.0.0.1:8000/

        当为‘admin/’时,网址为:http://127.0.0.1:8000/admin/

第二个参数:文件名.函数名。 

        我的后台逻辑写到views.py文件中的home函数中,那么这里应该是, views.home。

        views.py文件如下,其中,index.html为我要调用的html文件

from django.http import HttpResponse
from django.shortcuts import render


def home(request):
    return render(request, 'index.html')

# def home(request):
#     return HttpResponse("Hello, world. You're at the polls index.")

 第三个参数:即name,一般设置为函数名即可。

settings.py 文件:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['10.193.33.139']

这是设置是否开启测试模式,默认DEBUG=True。当DEBUG=False时,ALLOWED_HOSTS不能为空。ALLOWED_HOSTS列表默认为空。当启动服务时,我想修改默认ip地址(默认为127.0.0.1)时,比如修改为我本机地址(10.192.11.11)。那么我需要在此列表中加入我本机的IP地址,同时启动命令为:python manage.py runserver 10.192.11.11:8080

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'html/')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

这里面DIRS列表,就是存放html文件的路径。比如我的index.html 文件放在项目根目录下的html文件夹内,那这里就是“os.path.join(BASE_DIR, 'html/')”

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'html/static'),
)

“STATICFILES_DIRS”参数用来设置静态文件存放地址,比如html文件用到的各种css、js文件等。

LANGUAGE_CODE = 'en-us'

# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

这是设置服务的时区以及语言

参考:Writing your first Django app, part 1 | Django documentation | Django (djangoproject.com)https://docs.djangoproject.com/en/3.2/intro/tutorial01/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值