®
The World’s Top Talent, On Demand ®

Copyright 2010 - 2024 Toptal, LLC

\n\n \n\n\n\n

We’ll extend the base template for our homepage app in a new file, src/templates/homepage/index.html:

\n\n
{% extends \"layouts/base.html\" %}\n\n{% block main %}\n  
\n
\n
\n
\n

Hello, visitor {{ visit_counter.count }}!

\n
\n
\n
\n
\n{% endblock %}\n
\n\n

创建UI的最后一步是告诉Django在哪里可以找到这些模板. Let’s add a TEMPLATES['DIRS'] dictionary item to our settings.py file:

\n\n
# src/hello_visitor/settings.py\nTEMPLATES = [\n    {\n        ...\n        'DIRS': [BASE_DIR / 'templates'],\n        ...\n    },\n]\n
\n\n

我们的用户界面现在已经实现,我们几乎准备好测试应用程序的功能了. Before we do our testing, 我们需要准备好环境的最后一部分:静态内容缓存.

\n\n

Our Static Content Configuration

\n\n

避免在我们的开发系统上走架构上的捷径, 我们将配置静态内容缓存来反映我们的生产环境.

\n\n

我们将把项目的所有静态文件保存在一个目录中, src/static,并指示Django在部署前收集这些文件.

\n\n

We’ll use Toptal’s logo for our application’s favicon and store it as src/static/favicon.ico:

\n\n
# from `src` folder\nmkdir static\ncd static\nwget http://frontier-assets.shyayazuche.com/83b2f6e0d02cdb3d951a75bd07ee4058.png\nmv 83b2f6e0d02cdb3d951a75bd07ee4058.png favicon.ico\n
\n\n

接下来,我们将配置Django来收集静态文件:

\n\n
# src/hello_visitor/settings.py\n# Static files (CSS, JavaScript, images)\n# a la http://docs.djangoproject.com/en/3.2/howto/static-files/\n#\n我们将存储静态文件的源位置\nSTATICFILES_DIRS = [BASE_DIR / \"static\"]\n#构建Django收集所有静态文件的输出位置\nSTATIC_ROOT = BASE_DIR / \"staticfiles\"\nSTATIC_ROOT.mkdir(exist_ok=True)\n\n引用位于STATIC_ROOT中的静态文件时使用的# URL.\nSTATIC_URL = \"/static/\"\n\nSTATICFILES_STORAGE = \"whitenoise.storage.CompressedManifestStaticFilesStorage\"\n
\n\n

We only want to store our original static files in the source code repository; we do not want to store the production-optimized versions. Let’s add the latter to our .gitignore with this simple line:

\n\n
staticfiles\n
\n\n

我们的源代码库正确地存储了所需的文件, 现在我们需要配置缓存系统来处理这些静态文件.

\n\n

Static File Caching

\n\n

在生产环境中(因此在开发环境中也是如此),我们将使用 WhiteNoise 更有效地为Django应用程序的静态文件提供服务.

\n\n

我们将WhiteNoise注册为中间件,方法是向我们的 src/hello_visitor/settings.py file. Registration order is strictly defined, and WhiteNoiseMiddleware must appear immediately after SecurityMiddleware:

\n\n
MIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'whitenoise.middleware.WhiteNoiseMiddleware',\n    # ...\n]\n\nSTATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'\n
\n\n

现在应该在我们的开发环境中配置静态文件缓存, enabling us to run our application.

\n\n

Running Our Development Server

\n\n

我们已经编写了一个完整的应用程序,现在可以用下面的命令启动Django的嵌入式开发web服务器:

\n\n
# in the `src` folder\npython manage.py runserver\n
\n\n

When we navigate to http://localhost:8000,每次刷新页面,计数都会增加:

\n\n

一个浏览器窗口,显示pydantic Django应用程序的主屏幕, which says, \"Hello, visitor!\" on one line and \"1\" on the next.

\n\n

现在,我们有了一个可以工作的应用程序,当我们刷新页面时,它将增加其访问计数.

\n\n

Ready To Deploy

\n\n

本教程涵盖了在美观的环境中创建工作应用程序所需的所有步骤 Django development environment that matches production. 在第3部分中,我们将介绍将应用程序部署到其生产环境. 我们的额外练习也值得探索一下,这些练习突出了Django和pydantic的优点:它们包含在 code-complete repository for this pydantic tutorial.

\n\n
\n\n

Toptal Engineering博客向 Stephen Davidson 为了检查和测试本文中提供的代码示例.

\n","as":"div","isContentFit":true,"sharingWidget":{"url":"http://e1m.shyayazuche.com/django/optimize-your-environment-for-development-and-production-a-pydantic-tutorial-part-2","title":"Optimized Development Environment: Pydantic Tutorial, Part 2","text":null,"providers":["linkedin","twitter","facebook"],"gaCategory":null,"domain":{"name":"developers","title":"Engineering","vertical":{"name":"developers","title":"Developers","publicUrl":"http://e1m.shyayazuche.com/developers"},"publicUrl":"http://e1m.shyayazuche.com/developers/blog"},"hashtags":"Python,Django,Cloud"}}