栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django 一些 简单 配置

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django 一些 简单 配置

 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/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号