How to add a static resource path for aiohttp

Source: Internet
Author: User
This article describes how to add static resource paths to aiohttp. The so-called static resource refers to images, js, css, and other files. The official description is here.
A small project is used to describe the directory structure of the project:

.├── static│   ├── css│   │   ├── base.css│   │   ├── bootstrap.min.css│   │   └── font-awesome.min.css│   ├── font│   │   ├── FontAwesome.otf│   │   ├── fontawesome-webfont.eot│   │   ├── fontawesome-webfont.svg│   │   ├── fontawesome-webfont.ttf│   │   └── fontawesome-webfont.woff│   └── index.html└── proxy_server.py

Inproxy_server.py2 Static file directoriesstatic/cssAndstatic/fontAdd route:

 app.router.add_static('/css/',                       path='static/css',                       name='css') app.router.add_static('/font/',                       path='static/font',                       name='font')

Let's take a lookadd_staticMethod definition:

def add_static(self, prefix, path, *, name=None, expect_handler=None,                   chunk_size=256*1024, response_factory=StreamResponse,                   show_index=False, follow_symlinks=False):        """Add static files view.        prefix - url prefix        path - folder with files        """        # TODO: implement via PrefixedResource, not ResourceAdapter        assert prefix.startswith('/')        if prefix.endswith('/'):            prefix = prefix[:-1]        resource = StaticResource(prefix, path,                                  name=name,                                  expect_handler=expect_handler,                                  chunk_size=chunk_size,                                  response_factory=response_factory,                                  show_index=show_index,                                  follow_symlinks=follow_symlinks)        self.register_resource(resource)        return resource

Required two parameters:
prefix: It is a static fileurlTo/It is displayed in the address bar of the browser after the website host.index.htmlReference static pages
path: The path of the static file directory, which can be a relative path. the above code usesstatic/cssIs relative path -- relativeproxy_server.pyPath.
The following figure shows the page effect:
Load isindex.htmlThe following code references static resources:

 
 
 
 
 
 

AddfontIs because/font-awesome.min.cssUse:
If you modify the prefix:

 app.router.add_static('/css2017/',                       path='static/css',                       name='css')

The page is changed:
Modifyindex.htmlReference path of css in:

 
 
 
 
 
 

Although the directory itself is stillcss, But throughadd_staticIt is already consideredcss2017The page returns normally:
BecausestaticThe directory does not exist.css2017This folder.

Now we knowadd_staticYou can redefineprefixThe parameter can also hide the directory where static resources are stored on the server, or unify the resource files scattered in different places to the same path prefix.

In additionshow_index=TrueTo display the directory indexes of static resources. by default, access is prohibited:

app.router.add_static('/css2017/',                       path='static/css',                       name='css',                       show_index=True)

The preceding section describes how to add a static resource path to aiohttp. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.