Truck Signs App
A modern, containerized App built with Django, designed to be deployed via Docker.
Table of Contents
Introduction
This full-stack application provides a backend service for managing a truck signs webshop. The system uses PostgreSQL as the database and is ready to be deployed via Docker with a custom (optional) .env configuration.
Prerequisites
- A V-Server running Ubuntu/Debian
- Docker
Ensure your system is up to date:
sudo apt update && sudo apt install -y docker.io
Quickstart
Install dependencies:
sudo apt update && sudo apt install -y docker.io gitThis setup uses plain
docker network/docker runcommands, not Docker Compose — no Compose package is required.Clone the repository:
git clone git@github.com:BenjaminTietz/truck_signs_api.git cd truck_signs_apiGenerate and configure the .env file:
The environment file will be created automatically from env.template. Adjust the values to match your setup (optional):cp truck_signs_designs/settings/simple_env_config.env .env nano .env (optional)Build the Docker image:
docker build -t trucksigns-app .Create Dockernetwork
docker network create trucks-netStart the database container: (optional adjust values to match your setup)
docker run -d \ --name db \ --network trucks-net \ -e POSTGRES_DB=trucksigns_db \ -e POSTGRES_USER=trucksigns_user \ -e POSTGRES_PASSWORD=supertrucksignsuser! \ --restart on-failure \ -v trucksigns_pg_data:/var/lib/postgresql/data \ postgresThe database deliberately publishes no host port. The app container reaches it by container name over the
trucks-netnetwork, so nothing has to be exposed on the host. On a public server, do not publish port 5432 to the internet. On your local development machine you can add-p 127.0.0.1:5432:5432if you need a client such as DBeaver — the127.0.0.1:prefix keeps the port on the loopback interface instead of every interface.Start the app container:(optional adujust values to match your setup)
docker run -d \ --name web \ --network trucks-net \ -p 8020:8020 \ --env-file .env \ -e ALLOWED_HOSTS=<your_ip> \ --restart on-failure \ trucksigns-appLog in to the admin panel:
http://<your-server-ip>:8020/admin
Step 8 sends the Django admin password over plain HTTP, and every following request carries the session cookie unencrypted. The same applies to the API itself once it handles order or payment data. On a server reachable from the internet, publish the app port on the loopback interface only (-p 127.0.0.1:8020:8020), put a reverse proxy (nginx, Caddy, Traefik) in front of it and let the proxy terminate TLS with a certificate from a public CA (Let's Encrypt). Django additionally needs SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") to recognise the forwarded requests as secure, SECURE_SSL_REDIRECT, SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE set to True, and the public HTTPS origin listed in CSRF_TRUSTED_ORIGINS.
Usage
Environment Variables
The application uses environment variables to configure certain aspects of the system. These can be set in the .env file:
Managing the Database
Run migrations manually inside the container if needed:
docker exec -it web python manage.py migrate
To create a new Django superuser manually:
docker exec -it web python manage.py createsuperuser
Collecting Static Files
If you update static files and need to collect them again, run:
docker exec -it web python manage.py collectstatic --noinput
Stopping and Restarting the Container
To stop the container:
docker stop web
To restart it:
docker start web
To remove the container completely:
docker rm web
To rebuild and restart:
docker build -t trucksigns-app .
docker run -d \
--name web \
--network trucks-net \
-p 8020:8020 \
-v $(pwd)/.env:/app/.env:ro \
trucksigns-app
Signs for Trucks
The badges above state the versions the original project was built against, and both are past end of life: Python 3.8 reached end of life in October 2024, Django 2.2 LTS ended extended support on 11 April 2022 and receives no further security fixes. The section below is kept as the original project documentation; treat it as a historical record, not as a current deployment instruction.
Table of Contents
Description
Signs for Trucks is an online store to buy pre-designed vinyls with custom lines of letters (often call truck letterings). The store also allows clients to upload their own designs and to customize them on the website as well. Aside from the vinyls that are the main product of the store, clients can also purchase simple lettering vinyls with no truck logo, a fire extinguisher vinyl, and/or a vinyl with only the truck unit number (or another number selected by the client).
Settings
The settings folder inside the trucks_signs_designs folder contains the different setting's configuration for each environment (so far the environments are development, docker testing, and production). Those files are extensions of the base.py file which contains the basic configuration shared among the different environments (for example, the value of the template directory location). In addition, the .env file inside this folder has the environment variables that are mostly sensitive information and should always be configured before use. By default, the environment in use is the decker testing. To change between environments modify the __init.py__ file.
Models
Most of the models do what can be inferred from their name. The following dots are notes about some of the models to make clearer their propose:
- Category Model: The category of the vinyls in the store. It contains the title of the category as well as the basic properties shared among products that belong to a same category. For example, Truck Logo is a category for all vinyls that has a logo of a truck plus some lines of letterings (note that the vinyls are instances of the model Product). Another category is Fire Extinguisher, that is for all vinyls that has a logo of a fire extinguisher.
- Lettering Item Category: This is the category of the lettering, for example: Company Name, VIM NUMBER, ... Each has a different pricing.
- Lettering Item Variations: This contains a foreign key to the Lettering Item Category and the text added by the client.
- Product Variation: This model has the original product as a foreign key, plus the lettering lines (instances of the Lettering Item Variations model) added by the client.
- Order: Contains the cart (in this case the cart is just a vinyl as only one product can be purchased each time). It also contains the contact and shipping information of the client.
- Payment: It has the payment information such as the time of the purchase and the client id in Stripe.
To manage the payments, the payment gateway in use is Stripe.
Brief Explanation of the Views
Most of the views are CBV imported from rest_framework.generics, and they allow the backend api to do the basic CRUD operations expected, and so they inherit from the ListAPIView, CreateAPIView, RetrieveAPIView, ..., and so on.
The behavior of some of the views had to be modified to address functionalities such as creation of order and payment, as in this case, for example, both functionalities are implemented in the same view, and so a GenericAPIView was the view from which it inherits. Another example of this is the UploadCustomerImage View that takes the vinyl template uploaded by the clients and creates a new product based on it.
Installation
Clone the repo:
git clone <INSERT URL>Configure a virtual env and set up the database. See Link for configuring Virtual Environment and Link for Database setup.
Configure the environment variables.
Copy the content of the example env file that is inside the truck_signs_designs folder into a .env file:
cd truck_signs_designs/settings cp simple_env_config.env .envThe new .env file should contain all the environment variables necessary to run all the django app in all the environments. However, the only needed variables for the development environment to run are the following:
SECRET_KEY DB_NAME DB_USER DB_PASSWORD DB_HOST DB_PORT STRIPE_PUBLISHABLE_KEY STRIPE_SECRET_KEY EMAIL_HOST_USER EMAIL_HOST_PASSWORDFor the database, the default configurations should be:
DB_NAME=trucksigns_db DB_USER=trucksigns_user DB_PASSWORD=supertrucksignsuser! DB_HOST=localhost DB_PORT=5432The SECRET_KEY is the django secret key. To generate a new one see: Stackoverflow Link
The STRIPE_PUBLISHABLE_KEY and the STRIPE_SECRET_KEY can be obtained from a developer account in Stripe.
- To retrieve the keys from a Stripe developer account follow the next instructions:
- Log in into your Stripe developer account (stripe.com) or create a new one (stripe.com > Sign Up). This should redirect to the account's Dashboard.
- Go to Developer > API Keys, and copy both the Publishable Key and the Secret Key.
- To retrieve the keys from a Stripe developer account follow the next instructions:
The EMAIL_HOST_USER and the EMAIL_HOST_PASSWORD are the credentials to send emails from the website when a client makes a purchase. This is currently disable, but the code to activate this can be found in views.py in the create order view as comments. Therefore, any valid email and password will work.
Run the migrations and then the app:
python manage.py migrate python manage.py runserverCongratulations =) !!! The App should be running in localhost:8000
(Optional step) To create a super user run:
python manage.py createsuperuser
NOTE: To create Truck vinyls with Truck logos in them, first create the Category Truck Sign, and then the Product (can have any name). This is to make sure the frontend retrieves the Truck vinyls for display in the Product Grid as it only fetches the products of the category Truck Sign.
Useful Links
Postgresql Database
- Setup Database: Digital Ocean Link for Django Deployment on VPS
Docker
- Docker Oficial Documentation
- Dockerizing Django, PostgreSQL, guinicorn, and Nginx:
Django and DRF
- Django Official Documentation
- Generate a new secret key: Stackoverflow Link
- Modify the Django Admin:
- Small modifications (add searching, columns, ...): Link
- Modify Templates and css: Link from Medium
- Django Rest Framework Official Documentation
- More about Nested Serializers: Stackoverflow Link
- More about GenericViews: Testdriver.io Link
Miscellaneous
- Create Virual Environment with Virtualenv and Virtualenvwrapper: Link
- Configure CORS
- Setup Django with Cloudinary