DEV Community

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

Posted on • Edited on

3 2

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay