DEV Community

Cover image for Building Blog with Bocadillo - Settings
Itachi Uchiha
Itachi Uchiha

Posted on • Updated on

Building Blog with Bocadillo - Settings

GitHub Repository for this series: https://github.com/aligoren/bocadillo_blog


Using Bocadillo, you may need to configure something. For example, you want to set allowed hosts for your API calls. .env files are good to save credentials.

This is a .env file;

DEBUG=True
DATABASE_URL=postgresql://localhost/myproject
SECRET_KEY=43n080musdfjt54t-09sdgr
ALLOWED_HOSTS=127.0.0.1, localhost
Enter fullscreen mode Exit fullscreen mode

Don't commit .env file to your git project.

settings.py File

Actually, the name doesn't matter. But we chose this name to easy understanding. You will store your constants like MailChimp secret key.

For example, we set a default title for this blog and get it.

We'll create settings.py under the root folder.

from starlette.config import Config

config = Config(".env")

REDIRECT_TRAILING_SLASH = True
Enter fullscreen mode Exit fullscreen mode

We'll also create a .env file.

Title="Dev.to Blog"
Enter fullscreen mode Exit fullscreen mode

For example, we want to access settings from the app.py file. We need to import it.

from bocadillo import App

import settings
Enter fullscreen mode Exit fullscreen mode

Now our app will change like that;

asgi.py file

from bocadillo import configure
from blog.app import app

import settings

configure(app, settings)
Enter fullscreen mode Exit fullscreen mode

blog/app.py file

from bocadillo import App

import settings


app = App()

@app.route("/")
async def index(req, res):
    title = settings.config.get("Title")
    res.text = f"Site Title: {title}"
Enter fullscreen mode Exit fullscreen mode

We can run our project now.

This isn't important yet. But we will need to use settings. Because we want to write reusable components. Our philosophy should be "write once, use everywhere".

You may want to separate layers of your project. Like data, entities, presentation, etc.

You can skip this instruction.

Top comments (0)