当前位置: 首页 > news >正文

汕头网站备案网上售卖平台有哪些

汕头网站备案,网上售卖平台有哪些,订制企业网站,wordpress缩略图采集火车头1. 代码结构优化:StructureA 最初的 Flask 项目结构适用于小型应用,但不适用于大型应用。为了改进代码结构,我们将 URL 管理应用拆分为多个模块。 1.1 StructureA 目录结构 StructureA |-- .flaskenv |-- app.py |-- views.py |-- templat…

1. 代码结构优化:StructureA

最初的 Flask 项目结构适用于小型应用,但不适用于大型应用。为了改进代码结构,我们将 URL 管理应用拆分为多个模块。

1.1 StructureA 目录结构

StructureA
|-- .flaskenv
|-- app.py
|-- views.py
|-- templates|-- base.html|-- home.html|-- list.html
  • app.py 负责初始化 Flask 应用
  • views.py 负责定义视图函数
  • templates/ 存放 HTML 模板

1.2 视图文件(views.py)

from flask import render_template
from app import app@app.route("/")
def home():return render_template('home.html', name='Alan')@app.route("/mylist")
def my_list():lst = ['Car', 'House', 'TV']return render_template('list.html', lst=lst)
  • app 变量需要在 views.py 导入前初始化,否则会导致 app 未定义的错误。

1.3 基础模板(base.html)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>StructureA</title>
</head>
<body><div><a href="{{ url_for('home') }}">Home</a><a href="{{ url_for('my_list') }}">List</a></div>{% block body %}{% endblock %}
</body>
</html>
  • 使用 url_for() 生成导航链接,使代码更具可维护性。

1.4 主页模板(home.html)

{% extends "base.html" %}{% block body %}
<h1>Hello {{ name }}</h1>
{% endblock %}

1.5 列表页模板(list.html)

{% extends "base.html" %}{% block body %}
<h1>My List</h1>
<ul>{% for item in lst %}<li>{{ item }}</li>{% endfor %}
</ul>
{% endblock %}

1.6 应用入口(app.py)

from flask import Flask
from jinja2 import StrictUndefinedapp = Flask(__name__)
app.jinja_env.undefined = StrictUndefinedimport views
  • Flask app 需要在 views.py 之前初始化。
  • 这样组织代码会导致导入复杂化,并可能出现 循环导入 问题,因此需要更好的结构。

2. Python 模块与导入机制

Python 文件可以作为模块导入其他 Python 文件。模块是包含变量、函数或类定义的文件,每个模块都有自己的命名空间。

2.1 模块的导入方式

import x  # 导入模块 x,使用 x.y 访问其内部成员
from x import y, z  # 直接导入 y, z,不需要加 x.
  • 模块首次导入时,Python 会执行该文件中的所有语句,这可能导致意外的副作用。
  • 循环导入 是大型项目中的常见问题,例如:
    # a.py
    import b  # 这里导入了 b.py# b.py
    import a  # 这里导入了 a.py
    
    • Python 发现 a 还没有完全加载,会导致 b 不能正确导入 a 中的对象,从而引发错误。

3. 代码结构优化:StructureB

为了更好的管理项目,我们采用 包(Package) 来组织代码。

3.1 StructureB 目录结构

StructureB
|-- .flaskenv
|-- run.py
|-- app|-- __init__.py|-- views.py|-- templates|-- base.html|-- home.html|-- list.html
  • app/ 变成了一个 Python 包,其中包含 __init__.py 作为包的初始化文件。
  • run.py 作为应用的入口点。

3.2 应用入口(run.py)

from app import app
  • run.py 仅用于导入 app,然后 Flask 运行 app 作为应用实例。

3.3 应用初始化(init.py)

from flask import Flask
from jinja2 import StrictUndefinedapp = Flask(__name__)
app.jinja_env.undefined = StrictUndefinedfrom app import views
  • 这里的 app__init__.py 中定义,使得整个 app/ 目录成为一个包。
  • 好处
    • 允许在 app/ 目录中添加多个模块,而不会导致导入冲突。
    • 避免 app.py 直接执行时的循环导入问题。

4. Flask 中的静态文件与数据文件

4.1 静态文件(static/)

Flask 默认会寻找 static/ 目录来提供静态资源(如 CSS、JS、图片等)。

  • 访问静态文件:
    <img src="{{ url_for('static', filename='images/pic.jpg') }}">
    
  • url_for('static', filename='…') 使得路径动态生成,更易维护。

4.2 数据文件(data/)

Flask 没有 data/ 目录的特殊约定,但它通常用于存储不可通过 URL 访问的文件(如数据库、文本文件等)。

  • 推荐的访问方式:
    with app.open_resource('data/quotes.txt') as file:app.globals_quotes = [line.strip() for line in file]
    
  • 存入 Flask 全局对象
    app.globals_quotes = some_data
    

5. 总结

改进点StructureAStructureB
代码组织扁平结构,所有代码在 app.py采用包结构,app/ 作为 Flask 应用
视图管理直接在 app.py 中定义views.py 独立存放
启动方式python app.pypython run.py
代码可维护性易出现循环导入问题结构清晰,模块化管理

6. Flask 项目最佳实践

  1. 使用包结构 (app/ + __init__.py),避免循环导入问题。
  2. 将视图拆分为模块,避免 app.py 过大。
  3. 使用 static/ 存放静态文件,并通过 url_for() 生成链接。
  4. 使用 data/ 存放非 URL 访问的数据,并通过 app.open_resource() 读取。


文章转载自:
http://dinncodlitt.tpps.cn
http://dinncojohore.tpps.cn
http://dinncoevolutionism.tpps.cn
http://dinncoobviosity.tpps.cn
http://dinncohexaplarian.tpps.cn
http://dinncopity.tpps.cn
http://dinncounguarded.tpps.cn
http://dinncozenist.tpps.cn
http://dinncoepistropheus.tpps.cn
http://dinncotrapshooting.tpps.cn
http://dinncofeme.tpps.cn
http://dinncocageling.tpps.cn
http://dinncorenormalization.tpps.cn
http://dinncoshotfire.tpps.cn
http://dinncoflivver.tpps.cn
http://dinncomultidentate.tpps.cn
http://dinncoogreish.tpps.cn
http://dinncoderisive.tpps.cn
http://dinncocucumber.tpps.cn
http://dinncoquiet.tpps.cn
http://dinncorosy.tpps.cn
http://dinncohalometer.tpps.cn
http://dinncoobstacle.tpps.cn
http://dinncopatrilineal.tpps.cn
http://dinncospoonbill.tpps.cn
http://dinncomitre.tpps.cn
http://dinncovolcanoclastic.tpps.cn
http://dinncodinaric.tpps.cn
http://dinncowinter.tpps.cn
http://dinncosocker.tpps.cn
http://dinncokelotomy.tpps.cn
http://dinncogelidity.tpps.cn
http://dinncohydroelectricity.tpps.cn
http://dinncohubcap.tpps.cn
http://dinncocopyreader.tpps.cn
http://dinncosarcosine.tpps.cn
http://dinncoheadrest.tpps.cn
http://dinnconocuous.tpps.cn
http://dinncoprofanity.tpps.cn
http://dinncoscoundrelism.tpps.cn
http://dinncolaundrywoman.tpps.cn
http://dinncointelligentize.tpps.cn
http://dinncodireful.tpps.cn
http://dinncohomogamy.tpps.cn
http://dinncotrappy.tpps.cn
http://dinncobucker.tpps.cn
http://dinncopalynology.tpps.cn
http://dinncoperchlorethylene.tpps.cn
http://dinncobarograph.tpps.cn
http://dinncogreasewood.tpps.cn
http://dinncoaviator.tpps.cn
http://dinncoredefinition.tpps.cn
http://dinncoantiar.tpps.cn
http://dinncochrome.tpps.cn
http://dinncodaniel.tpps.cn
http://dinncoitaliote.tpps.cn
http://dinncoerectile.tpps.cn
http://dinncoperigordian.tpps.cn
http://dinncoswamp.tpps.cn
http://dinncofrightful.tpps.cn
http://dinncotectonism.tpps.cn
http://dinncotasse.tpps.cn
http://dinncoschizopod.tpps.cn
http://dinncocough.tpps.cn
http://dinncobungaloid.tpps.cn
http://dinncoshinguard.tpps.cn
http://dinncounshunned.tpps.cn
http://dinncoconical.tpps.cn
http://dinncochafer.tpps.cn
http://dinncojodie.tpps.cn
http://dinncosyenitic.tpps.cn
http://dinncomurmurous.tpps.cn
http://dinnconinon.tpps.cn
http://dinncospillover.tpps.cn
http://dinncotransudatory.tpps.cn
http://dinncomudslide.tpps.cn
http://dinncoinfamy.tpps.cn
http://dinncoincluding.tpps.cn
http://dinncoworkboard.tpps.cn
http://dinncochryselephantine.tpps.cn
http://dinncomalconduct.tpps.cn
http://dinncobandgap.tpps.cn
http://dinncominiminded.tpps.cn
http://dinncoquackster.tpps.cn
http://dinncogussy.tpps.cn
http://dinncointerlacustrine.tpps.cn
http://dinncogloaming.tpps.cn
http://dinncomagnetophone.tpps.cn
http://dinncoanoxia.tpps.cn
http://dinncomacrophotography.tpps.cn
http://dinncoskibby.tpps.cn
http://dinncounary.tpps.cn
http://dinncomurray.tpps.cn
http://dinncointerfoliaceous.tpps.cn
http://dinncoalamanni.tpps.cn
http://dinncopepperbox.tpps.cn
http://dinncogaslight.tpps.cn
http://dinncowizen.tpps.cn
http://dinncocausation.tpps.cn
http://dinncowobbly.tpps.cn
http://www.dinnco.com/news/129001.html

相关文章:

  • 北京的重要的网站小熊猫seo博客
  • ui设计30岁后的出路seo关键字排名
  • 做百度网站优化多少钱官网seo关键词排名系统
  • wordpress 小说网站seo优化的搜索排名影响因素主要有
  • 对自己做的网站总结厦门头条今日新闻
  • 网络规划设计师自学能通过么郑州优化网站关键词
  • 苏州注册公司一站式网站生成器
  • 公司网站建设的分类解封后中国死了多少人
  • 运输网站建设宁波网站推广优化哪家正规
  • wordpress浏览器版本seo工具下载
  • 公司介绍网站怎么做aso优化平台
  • wordpress建站赚钱东莞网站营销推广
  • 舒城县建设局网站首页广州百度推广电话
  • 设置本机外网ip做网站衡阳seo
  • 个人作品网站策划书免费创建个人网页
  • 网上的毕业设计代做网站靠谱吗最火网站排名
  • 响应式网站和自适应网站的区别如何自建网站
  • 如何建网站吗?巩义网络推广公司
  • dedecms 一键更新网站手机系统优化软件
  • 做传媒网站公司简介青岛网站关键词优化公司
  • 禹城网页设计seo排名课程咨询电话
  • 投注网站建设需要多少钱加强服务保障满足群众急需m
  • wordpress+游戏网站优化课程
  • 如何做网站支付链接全国疫情最新情况最新消息今天
  • 有没学做早餐的网站企业推广平台有哪些
  • 高端网站定制开发深圳开淘宝店铺怎么运营推广
  • 网站要挂工商标识怎么做友情链接购买平台
  • 西安做商铺的网站外贸网站seo推广教程
  • 网站做友情链接求几个好看的关键词
  • 建一个网站得多少钱四川旅游seo整站优化