|
| 1 | +A static file is a type of file that does not change often and is not generated by a server-side script. Examples of static files include images, CSS and JavaScript files, audio and video files, and other types of media. |
| 2 | + |
| 3 | +Static files in Ellar are served using the `StaticFiles` ASGI class, which is an extension of the Starlette `StaticFiles` ASGI class. |
| 4 | +This class uses the static files specified in the application's **modules** and **configuration**. |
| 5 | + |
| 6 | +In addition, Ellar creates a route that mounts the static server at the `/static` path. |
| 7 | +The path can be modified by providing a new value for the `STATIC_MOUNT_PATH` configuration variable. |
| 8 | + |
| 9 | +## **Configuring static files** |
| 10 | + |
| 11 | +1. In your config file, define `STATIC_MOUNT_PATH`, for example: |
| 12 | + ```python |
| 13 | + |
| 14 | + class Config: |
| 15 | + STATIC_MOUNT_PATH = '/static' |
| 16 | + ``` |
| 17 | + |
| 18 | +2. Store your static files in a folder called **static** in your module. For example **my_module/static/my_module/example.jpg**. |
| 19 | +3. In your templates, use the `url_for` with `static` and `path` parameter to build the URL for the given relative path using the configured in `STATIC_DIRECTORIES`, `STATIC_FOLDER_PACKAGES` or Module. |
| 20 | + ```html |
| 21 | + <img src="{{url_for('static', path='my_module/example.jpg')}}" alt="My image"> |
| 22 | + ``` |
| 23 | + OR, visit `/static/my_app/example.jpg` |
| 24 | + |
| 25 | + |
| 26 | +## **Static File in Modules** |
| 27 | + |
| 28 | +Managing multiple sets of static files in larger projects can be challenging, |
| 29 | +but by organizing each set of static files within a specific module, |
| 30 | +it becomes easier to manage and maintain. |
| 31 | +This approach allows for clear organization and separation of static assets, |
| 32 | +making it more manageable in a large project. |
| 33 | + |
| 34 | +In our previous project, within the `dogs` module folder, we can create a following directories, `my_static/dogs`. |
| 35 | +Inside this folder `my_static/dogs`, we can create a file named `example.txt`. |
| 36 | +This allows us to keep all of the static files related to the dogs module organized in one location `my_static`. |
| 37 | + |
| 38 | +Next, we tell `DogModule` about our static folder. |
| 39 | + |
| 40 | +```python |
| 41 | +# project_name/apps/dogs/module.py |
| 42 | + |
| 43 | +from ellar.common import Module |
| 44 | +from ellar.core import ModuleBase |
| 45 | +from ellar.di import Container |
| 46 | + |
| 47 | +from .controllers import DogsController |
| 48 | + |
| 49 | + |
| 50 | +@Module( |
| 51 | + controllers=[DogsController], static_folder='my_static' |
| 52 | +) |
| 53 | +class DogsModule(ModuleBase): |
| 54 | + def register_providers(self, container: Container) -> None: |
| 55 | + # for more complicated provider registrations |
| 56 | + # container.register_instance(...) |
| 57 | + pass |
| 58 | +``` |
| 59 | + |
| 60 | +## **Other Static Configurations** |
| 61 | +In addition to setting static directories within modules, |
| 62 | +it is also possible to manually specify additional static directories that are not located within a module by using the |
| 63 | +`STATIC_FOLDER_PACKAGES` and `STATIC_DIRECTORIES` variables in the application's configuration. |
| 64 | +These variables allow for even more flexibility in organizing and managing static files in a project. |
| 65 | +These directories will be served by the StaticFiles ASGI class along with the module-scoped static files. |
| 66 | + |
| 67 | +### `STATIC_DIRECTORIES` |
| 68 | +`STATIC_DIRECTORIES` variable is a list of directories within the project that contain static files. |
| 69 | +These directories are not necessarily scoped to a specific module and can be used to serve static files from any location within the project. |
| 70 | +These directories can be added to the `STATIC_DIRECTORIES` list in the application's configuration. |
| 71 | + |
| 72 | +```python |
| 73 | +STATIC_DIRECTORIES = ['project_name/static-files', 'project_name/path/to/static/files'] |
| 74 | +``` |
| 75 | + |
| 76 | +### `STATIC_FOLDER_PACKAGES` |
| 77 | +`STATIC_FOLDER_PACKAGES` variable is a list of tuples that contain python packages that hold some static files. |
| 78 | +These packages should have a `static` folder and the **package name** should be passed as tuple `(package_name, package_path)`, |
| 79 | +**package_path** is the relative path of static folder. |
| 80 | + |
| 81 | +```python |
| 82 | + |
| 83 | +STATIC_FOLDER_PACKAGES = [('bootstrap', 'statics'), ('package-name', 'path/to/static/directory')] |
| 84 | +``` |
| 85 | + |
| 86 | +Static files will respond with "404 Not found" or "405 Method not allowed" responses for requests which do not match. In `HTML` mode if `404.html` file exists it will be shown as 404 response. |
0 commit comments