ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 2639

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

 

13.3.2 Registering A View Callable to Serve a “Static” Asset . . . . . . . . . . . . . .

159

13.4

Overriding Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

160

 

13.4.1

The override_asset API . . . . . . . . . . . . . . . . . . . . . . . . . . .

160

14 Request and Response Objects

163

14.1

Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

164

 

14.1.1 Special Attributes Added to the Request by Pyramid . . . . . . . . . . . . . . .

165

 

14.1.2

URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

165

 

14.1.3

Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

165

 

14.1.4

Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

166

 

14.1.5

Multidict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

166

 

14.1.6 Dealing With A JSON-Encoded Request Body . . . . . . . . . . . . . . . . . .

166

 

14.1.7 Cleaning Up After a Request . . . . . . . . . . . . . . . . . . . . . . . . . . . .

168

 

14.1.8

More Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

168

14.2

Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

169

 

14.2.1

Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

170

 

14.2.2

Instantiating the Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

170

 

14.2.3

Exception Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

170

 

14.2.4

More Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

170

15 Sessions

 

171

15.1

Using The Default Session Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

171

15.2

Using a Session Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

172

15.3

Using Alternate Session Factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

173

15.4

Creating Your Own Session Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . .

174

15.5

Flash Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

174

 

15.5.1 Using the session.flash Method . . . . . . . . . . . . . . . . . . . . . . .

174

 

15.5.2 Using the session.pop_flash Method . . . . . . . . . . . . . . . . . . . .

175

 

15.5.3 Using the session.peek_flash Method . . . . . . . . . . . . . . . . . . .

175

15.6

Preventing Cross-Site Request Forgery Attacks . . . . . . . . . . . . . . . . . . . . . .

176

 

15.6.1 Using the session.get_csrf_token Method . . . . . . . . . . . . . . . .

176

 

15.6.2 Using the session.new_csrf_token Method . . . . . . . . . . . . . . . .

177

16 Using Events

179

16.1

Configuring an Event Listener Imperatively . . . . . . . . . . . . . . . . . . . . . . . .

179

16.2

Configuring an Event Listener Using a Decorator . . . . . . . . . . . . . . . . . . . . .

180

16.3

An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

181

17 Environment Variables and .ini File Settings

183

17.1

Reloading Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

184

17.2

Reloading Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

184

17.3

Debugging Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

184

17.4

Debugging Not Found Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

185

17.5

Debugging Route Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

185


17.6

Preventing HTTP Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

185

17.7

Debugging All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

185

17.8

Reloading All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

186

17.9

Default Locale Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

186

17.10Including Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

186

 

17.10.1 pyramid.includes vs. pyramid.config.Configurator.include()187

17.11Explicit Tween Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

188

 

17.11.1 PasteDeploy Configuration vs. Plain-Python Configuration . . . . . . . . . . . .

189

17.12Mako Template Render Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

190

 

17.12.1 Mako Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

190

 

17.12.2 Mako Module Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

190

 

17.12.3 Mako Input Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

190

 

17.12.4 Mako Error Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

191

 

17.12.5 Mako Default Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

191

 

17.12.6 Mako Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

191

 

17.12.7 Mako Strict Undefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

191

 

17.12.8 Mako Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

192

17.13Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

192

17.14Understanding the Distinction Between reload_templates and reload_assets

193

17.15Adding A Custom Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

194

18 Logging

 

197

18.1

Logging Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

197

18.2

Sending Logging Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

200

18.3

Filtering log messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

201

18.4

Advanced Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

202

18.5

Logging Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

202

18.6

Request Logging with Paste’s TransLogger . . . . . . . . . . . . . . . . . . . . . . . .

203

19 PasteDeploy Configuration Files

207

19.1

PasteDeploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

207

 

19.1.1 Entry Points and PasteDeploy .ini Files . . . . . . . . . . . . . . . . . . . . .

208

 

19.1.2 [DEFAULTS] Section of a PasteDeploy .ini File . . . . . . . . . . . . . . .

210

20 Command-Line Pyramid

211

20.1

Displaying Matching Views for a Given URL . . . . . . . . . . . . . . . . . . . . . . .

211

20.2

The Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

213

 

20.2.1

Extending the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

214

 

20.2.2

IPython or bpython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

216

20.3

Displaying All Application Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

216

20.4

Displaying “Tweens” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

217

20.5

Invoking a Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

218

20.6

Writing a Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

219

 

20.6.1

Changing the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

222


 

 

20.6.2

Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

222

 

 

20.6.3

Setting Up Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

223

 

20.7

Making Your Script into a Console Script . . . . . . . . . . . . . . . . . . . . . . . . .

223

21

Internationalization and Localization

229

 

21.1

Creating a Translation String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

229

 

 

21.1.1 Using The TranslationString Class . . . . . . . . . . . . . . . . . . . .

229

 

 

21.1.2 Using the TranslationStringFactory Class . . . . . . . . . . . . . . .

231

 

21.2

Working With gettext Translation Files . . . . . . . . . . . . . . . . . . . . . . . . .

232

 

 

21.2.1 Installing Babel and Lingua . . . . . . . . . . . . . . . . . . . . . . . . . . . .

233

 

 

21.2.2 Extracting Messages from Code and Templates . . . . . . . . . . . . . . . . . .

235

 

 

21.2.3 Initializing a Message Catalog File . . . . . . . . . . . . . . . . . . . . . . . . .

236

 

 

21.2.4 Updating a Catalog File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

237

 

 

21.2.5 Compiling a Message Catalog File . . . . . . . . . . . . . . . . . . . . . . . . .

237

 

21.3

Using a Localizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

238

 

 

21.3.1

Performing a Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

238

 

 

21.3.2

Performing a Pluralization . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

239

 

21.4

Obtaining the Locale Name for a Request . . . . . . . . . . . . . . . . . . . . . . . . .

240

 

21.5

Performing Date Formatting and Currency Formatting . . . . . . . . . . . . . . . . . .

240

 

21.6

Chameleon Template Support for Translation Strings . . . . . . . . . . . . . . . . . . .

241

 

21.7

Mako Pyramid I18N Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

242

 

21.8

Localization-Related Deployment Settings . . . . . . . . . . . . . . . . . . . . . . . . .

242

 

21.9

“Detecting” Available Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

243

 

21.10Activating Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

244

 

 

21.10.1 Adding a Translation Directory . . . . . . . . . . . . . . . . . . . . . . . . . .

244

 

 

21.10.2 Setting the Locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

245

 

21.11Locale Negotiators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

245

 

 

21.11.1 The Default Locale Negotiator . . . . . . . . . . . . . . . . . . . . . . . . . . .

246

 

 

21.11.2 Using a Custom Locale Negotiator . . . . . . . . . . . . . . . . . . . . . . . . .

246

22 Virtual Hosting

249

 

22.1

Hosting an Application Under a URL Prefix . . . . . . . . . . . . . . . . . . . . . . . .

249

 

22.2

Virtual Root Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

250

 

22.3

Further Documentation and Examples . . . . . . . . . . . . . . . . . . . . . . . . . . .

251

23

Unit, Integration, and Functional Testing

253

 

23.1

Test Set Up and Tear Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

254

 

 

23.1.1

What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

256

 

23.2

Using the Configurator and pyramid.testing APIs in Unit Tests . . . . . . . .

256

 

23.3

Creating Integration Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

258

 

23.4

Creating Functional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

259

24 Resources

 

261

 

24.1

Defining a Resource Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

262


24.2

Location-Aware Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

263

24.3

Generating The URL Of A Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . .

264

 

24.3.1 Overriding Resource URL Generation . . . . . . . . . . . . . . . . . . . . . . .

265

24.4

Generating the Path To a Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

266

24.5

Finding a Resource by Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

267

24.6

Obtaining the Lineage of a Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . .

268

24.7

Determining if a Resource is In The Lineage of Another Resource . . . . . . . . . . . .

268

24.8

Finding the Root Resource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

269

24.9

Resources Which Implement Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . .

269

24.10Finding a Resource With a Class or Interface in Lineage . . . . . . . . . . . . . . . . .

271

24.11Pyramid API Functions That Act Against Resources . . . . . . . . . . . . . . . . . . . .

272

25 Much Ado About Traversal

273

25.1

URL Dispatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

274

25.2

Historical Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

275

25.3

Traversal (aka Resource Location) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

275

25.4

What Is a “Resource”? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

276

25.5

View Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

277

25.6

Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

278

26 Traversal

281

26.1

Traversal Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

281

26.2

The Resource Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

283

26.3

The Traversal Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

285

 

26.3.1 A Description of The Traversal Algorithm . . . . . . . . . . . . . . . . . . . . .

285

 

26.3.2 Traversal Algorithm Examples . . . . . . . . . . . . . . . . . . . . . . . . . . .

288

 

26.3.3 Using Resource Interfaces In View Configuration . . . . . . . . . . . . . . . . .

290

26.4

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

292

27 Security

293

27.1

Enabling an Authorization Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

294

 

27.1.1 Enabling an Authorization Policy Imperatively . . . . . . . . . . . . . . . . . .

294

27.2

Protecting Views with Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295

 

27.2.1 Setting a Default Permission . . . . . . . . . . . . . . . . . . . . . . . . . . . .

296

27.3

Assigning ACLs to your Resource Objects . . . . . . . . . . . . . . . . . . . . . . . . .

296

27.4

Elements of an ACL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

297

27.5

Special Principal Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

299

27.6

Special Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

300

27.7

Special ACEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

300

27.8

ACL Inheritance and Location-Awareness . . . . . . . . . . . . . . . . . . . . . . . . .

300

27.9

Changing the Forbidden View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

301

27.10Debugging View Authorization Failures . . . . . . . . . . . . . . . . . . . . . . . . . .

301

27.11Debugging Imperative Authorization Failures . . . . . . . . . . . . . . . . . . . . . . .

302

27.12Creating Your Own Authentication Policy . . . . . . . . . . . . . . . . . . . . . . . . .

302


27.13Creating Your Own Authorization Policy . . . . . . . . . . . . . . . . . . . . . . . . .

. 303

28 Combining Traversal and URL Dispatch

305

28.1

A Review of Non-Hybrid Applications . . . . . . . . . . . . . . . . . . . . . . . . . .

. 305

 

28.1.1

URL Dispatch Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 306

 

28.1.2

Traversal Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 306

28.2

Hybrid Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 307

 

28.2.1 The Root Object for a Route Match . . . . . . . . . . . . . . . . . . . . . . .

. 308

 

28.2.2 Using *traverse In a Route Pattern . . . . . . . . . . . . . . . . . . . . .

. 308

 

28.2.3 Using the traverse Argument In a Route Definition . . . . . . . . . . . . .

. 312

 

28.2.4 Using *subpath in a Route Pattern . . . . . . . . . . . . . . . . . . . . . .

. 313

28.3

Corner Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 313

 

28.3.1 Registering a Default View for a Route That Has a view Attribute . . . . . . .

. 313

 

28.3.2

Binding Extra Views Against a Route Configuration that Doesn’t Have

a

 

 

*traverse Element In Its Pattern . . . . . . . . . . . . . . . . . . . . . . .

. 314

29 Using Hooks

 

317

29.1

Changing the Not Found View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 317

29.2

Changing the Forbidden View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 320

29.3

Changing the Request Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 321

29.4

Using The Before Render Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 322

29.5

Adding Renderer Globals (Deprecated) . . . . . . . . . . . . . . . . . . . . . . . . .

. 323

29.6

Using Response Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 324

29.7

Using Finished Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 325

29.8

Changing the Traverser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 326

29.9

Changing How pyramid.request.Request.resource_url() Generates a URL 327

29.10Changing How Pyramid Treats View Responses . . . . . . . . . . . . . . . . . . . . .

. 328

29.11Using a View Mapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 330

29.12Registering Configuration Decorators . . . . . . . . . . . . . . . . . . . . . . . . . .

. 332

29.13Registering “Tweens” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 334

 

29.13.1 Creating a Tween Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 334

 

29.13.2 Registering an Implicit Tween Factory . . . . . . . . . . . . . . . . . . . . . .

. 335

 

29.13.3 Suggesting Implicit Tween Ordering . . . . . . . . . . . . . . . . . . . . . . .

. 336

 

29.13.4 Explicit Tween Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 338

 

29.13.5 Tween Conflicts and Ordering Cycles . . . . . . . . . . . . . . . . . . . . . .

. 339

 

29.13.6 Displaying Tween Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 339

30 Pyramid Configuration Introspection

341

30.1

Using the Introspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 341

30.2

Introspectable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 342

30.3

Pyramid Introspection Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 343

30.4

Introspection in the Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 354

30.5

Disabling Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 354