Status Completed
Categories Web Design
Published on

This website is made in Python, with the Django framework. It's my personal website, an open-source project that can be used as a base for other projects.

This project is free to be copied, modified and sold, as long as this GitHub repository is credited. For other legal questions, check the license.

This project can be found in its GitHub repository: https://github.com/Lavedonio/site_lavedonio_django. For feedback, feature requests or bug reports, please open an Issue in the repository or get in touch, either through one of my social networks or through the website's contact page.

Current version: 0.1.0 (beta)

This project has:

  • A homepage with the list of featured projects and posts, plus links to the about and contact pages;
  • A static "About" page, saying a bit more about the author and the website;
  • A Projects page, a portfolio of relevant and featured projects;
  • A Blog page, with articles that can hold any HTML code at all;
  • A Contact page, protected from spam with reCAPTCHA, which sends an e-mail with the information given;
  • Image and file uploads and redirects;
  • A link shortener;
  • An admin site to manage the whole process.

To do:

  • Customizable dashboard;
  • Login page template;
  • WYSIWYG editor for the Blog and Projects;
  • Sendgrid as the sending e-mail of the Contact page.

Photos

Website homepage

Website homepage

Website contact page

Website contact page

To install and run this project on your machine, follow the instructions below.

Requirements

For the development environment:

  • Python 3.6 or later;
  • A Gmail account or another e-mail service.

For the staging environment:

  • Python 3.6 or later;
  • A Gmail account or another e-mail service;
  • reCAPTCHA set up;
  • An AWS account.

For the production environment:

  • Python 3.6 or later;
  • A Gmail account or another e-mail service;
  • reCAPTCHA set up;
  • An AWS account;
  • A Google Analytics account.

Download and initial setup

Get this project's files, either by downloading the ZIP file and unzipping it, by running git clone https://github.com/Lavedonio/site_lavedonio_django.git straight from this repository, or by making a Fork of it and running git clone from there.

Once it's downloaded, and assuming you already have Python 3.6 or later installed, open the Terminal (the Command Prompt on Windows), go to the project's folder and type the command:

pip install -r requirements.txt

If installing the psycopg2 package fails, you can remove it from the list in this txt file and run the command again (it's needed for Postgres, but not for the development environment).

Done! It can now run in the development environment. BUT... one more step is recommended (and required for the staging and production environments): setting up the secret and password variables. There are 2 ways to do it:

1. Set up the secrets.py file (recommended)

Open the project and go to the site_core/settings folder. Then copy the secrets_example.py file, paste it in the same folder and rename it to secrets.py.

Note: Rename the file to exactly secrets.py. This matters not only for the project to work, but also to make sure you don't commit your private keys, secrets and passwords by mistake for others to see.

Open the secrets.py file in a text editor and put your secrets and keys in their fields. (More details in the Secret variables section below.)

2. Set up environment variables

Although it isn't the best recommendation, some hosting services require every file of the project to be in its git repository (e.g. Heroku). That's bad, because private keys, secrets and passwords that get committed stay in the history forever, for anyone with access to the code to see. So, in that case, the alternative is to use environment variables.

macOS and Linux
  1. In the terminal, type cd and press Enter, to make sure you're in your home directory;
  2. Type the command sudo nano .bashrc and then your password;
  3. For each key of the secrets dictionary in the secrets_example.py file (in the site_core/settings folder), type the following command at the end of the file:
    export KEY="VALUE"
    Remember to replace KEY with the key being added (e.g. ENVIRONMENT, SECRET_KEY, SERVER_TYPE, etc...) and VALUE with its value (always between quotes), and not to leave spaces around the equals sign. Put one command per line.
  4. Save the file with Ctrl + X, then press Y and Enter to save the changes;
  5. Restart the terminal.
Windows
  1. Open the Control Panel;
  2. Go to System and Security and then to System;
  3. Click Advanced system settings;
  4. Click the Environment Variables... button;
  5. Under User variables for USER (USER being your computer's user name), click the New... button;
  6. For each key of the secrets dictionary in the secrets_example.py file (in the site_core/settings folder), type the dictionary's key in Variable name and its value in Variable value. Then click OK;
  7. Repeat steps 5 and 6 for as many variables as you want to add;
  8. Click OK in both open windows and close the Control Panel.
Heroku and similar hosting services

In this case, don't type the variables in the terminal. Look up how to add environment variables in these services.

Secret variables

Each variable has a specific purpose in the project. They're described below, grouped by type.

Category 1: Django

ENVIRONMENT

This variable says which environment the program is running in and, therefore, which set of settings must be imported.

Possible values: development, staging, production.

SECRET_KEY

This variable is very important for the security of the system's authentication. The development environment doesn't need it, since the code already has a fixed one (which must not be reused on servers).

For the staging and production environments, open the terminal on macOS and Linux, or the Command Prompt on Windows, and type the command:

python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"

Take the result and use it as the value of SECRET_KEY.

DEBUG_VALUE

This value sets whether Django shows the details of errors right in the browser. That's great for development, but terrible for production environments.

In the development environment, the code already sets this value to True, so this setting isn't needed. In the *staging* and production environments, this variable must be set for the system to run correctly.

In the production environment in particular, this value must be False, since the detailed error log can be a risk to the system's security.

Possible values: True, False.

SERVER_TYPE

This setting only has an effect in the *staging* and production environments.

It's specifically for Heroku and does all the needed setup automatically. For other kinds of servers, put the database's values in the DB_NAME, DB_USER and DB_PASS variables.

Possible values: heroku, Empty.

HTTPS_TRAFFIC_ONLY

This setting only has an effect in the staging and production environments.

It forces the website's traffic to use HTTPS instead of HTTP.

Possible values: True, False.

Category 2: E-mail

EMAIL_USER

The e-mail user that sends the messages of the Contact form. The code is set up for Gmail addresses. Other providers need changes to the code.

EMAIL_PASS

Your e-mail's password. For Gmail, there are 2 ways to do it:

  1. Turning on app passwords (see how here) and using the generated password;
  2. Turning on sign-in from less secure apps and using your regular password (not recommended).

Category 3: reCAPTCHA

Go to the reCAPTCHA website and add a site in the console. Fill in what's asked and make sure you're using version 3. Add localhost and 127.0.0.1 to the list of domains so you can test it in the development environment. You'll get 2 keys, to be used in the following variables.

Attention: this website uses version 3. If you create keys for version 2, the verification will fail.

RECAPTCHA_PUBLIC_KEY

In the reCAPTCHA console, this variable must be filled with the value of site_key.

RECAPTCHA_PRIVATE_KEY

In the reCAPTCHA console, this variable must be filled with the value of secret_key.

Category 4: Postgres Database

In the staging and production environments, it's recommended to use a sturdier database than the sqlite3 that Django uses by default. This project is set up to use Postgres outside the development environment.

If you're running on Heroku, these variables won't be used and can be left blank.

For the database to work, you need to install and set it up. To download and install it, I recommend following the steps on Postgres's downloads page. For the initial setup, I recommend reading the "Getting Started" section of the official manual. It should be enough for this project, since Django creates the tables itself.

Once the database is set up, fill in the following variables:

DB_NAME

Name of the database created.

DB_USER

Name of the user that accesses it.

DB_PASS

Password of the user in the variable above.

Category 5: AWS (Amazon Web Services)

To improve the website's performance and to get around limitations of services like Heroku, an external service is needed to deliver the media files uploaded by users.

To create and set up an account, I recommend watching this tutorial: Python Django Tutorial: Full-Featured Web App Part 13 - Using AWS S3 for File Uploads.

Once AWS is set up, fill in the following variables:

AWS_ACCESS_KEY_ID

In the user's downloaded credentials, look for the key with the same name as this variable.

AWS_SECRET_ACCESS_KEY

In the user's downloaded credentials, look for the key with the same name as this variable.

AWS_STORAGE_BUCKET_NAME

Name of the properly set up Bucket where Django should put the files.

Category 6: Google Analytics

Recommended for the production environment, although it's also available in staging, if you also want to track how that website is used.

Go to Google Analytics and create a new project. There's no need to copy the code, only the tracking ID. Use it to set the following variable:

ANALYTICS_ID

Google Analytics tracking ID.

Running in the development environment

With the project downloaded and set up, run the following commands in the Terminal (or Command Prompt):

  1. python manage.py migrate
  2. python manage.py createsuperuser (this command asks you to create a user name, e-mail and password)
  3. python manage.py runserver
  4. Open the browser and type localhost:8000 or 127.0.0.1:8000
  5. Enjoy!
  6. P.S.: go to localhost:8000/admin/ to add users, posts, projects, etc...

Deployment

Attention: Don't forget to change the values of ALLOWED_HOSTS in the staging/production settings to your own domains or IP addresses.

Heroku

To deploy this project to Heroku, follow the steps below:

  1. Create an account on Heroku;
  2. Install the Heroku CLI on your computer;
  3. Create an app with the command heroku apps:create example (replacing example with your app's name, which must be unique);
  4. Go to your dashboard and add the secret variables set up earlier in the Config Vars section;
  5. Run the command git push heroku master in the Terminal (or Command Prompt) in the project's folder.

An error may happen the first time the app goes live. That's because the django_heroku module can't find the database's initial settings. To fix it, in the site_core/settings folder, open staging.py or production.py (depending on which one you're deploying), copy the code that sets the DATABASES variable and paste it before the line with if config.get("SERVER_TYPE") == "heroku":. Commit it and run step 5 again. The website won't work, but the database will be created. Undo the change, commit it and run step 5 once more. After that, the website should work normally.

Linux Server

This is a much more complex process, with many steps. I recommend following this step-by-step tutorial by Corey Schafer, which explains it very well: Python Django Tutorial: Deploying Your Application (Option #1) - Deploy to a Linux Server.

On top of that, to set up a domain and an SSL certificate (to enable HTTPS), I also recommend these two videos by Corey Schafer:

Further reading

To learn more about this project and Django in general, I recommend these links:

« Back to the projects