Skip to content

The Ultimate Cheese Index - Django Crash Course

License

Notifications You must be signed in to change notification settings

ghaith96/everycheese

Repository files navigation

EveryCheese

The Ultimate Cheese Index!

My journy through Django Crash Course using Python 3.8 and Django 3.x book.

Summary

We started with a deployable project by using the django-crash- starter Cookiecutter template. We installed dependencies, cre- ated a database, and started up Django’s development server. Then we saved our progress using git.

Next, we explored authentication and the User model. We ex- panded upon the User model by adding a bio field. Then we learned about tests and test coverage.

Now we got into the cheesiness of things. We created a Cheeses app and a Cheese model. We generated the model migration and executed it, creating a table in our database. And we began writing our tests.

From here we expanded on our cheese model and began adding views so users could start adding their favorite cheeses. Work- ing with views meant learning about controlling access to views, adding links, buttons, multi-use templates, and third-party pack- ages like django-countries and django-crispy-forms.

Throughout all of this, we reinforced the idea of writing easily understood function-based tests powered by pytest. That came 273 with more knowledge about useful project ingredients like test factories and pytest fixtures.

Quick setup

The next steps assume that conda is already installed

1 - Create a conda environment:

conda create python=3.8 -n everycheese

2 - Activate the conda environment

conda activate everycheese

3 - Install the project basic dependencies and development dependencies

Make sure you are inside the root project directory before executing the next commands.

The root project directory is the directory that contains the manage.py file

On Linux and Mac

pip install -r requirements/local.txt

On Windows

pip install -r requirements\local.txt

4 - Configure the database connection string on the .env

On Linux and Mac

cp env.sample.mac_or_linux .env

On Windows

copy env.sample.windows .env

Change the value of the variable DATABASE_URL inside the file .env with the information of the database we want to connect.

Note: Several project settings have been configured so that they can be easily manipulated using environment variables or a plain text configuration file, such as the .env file. This is done with the help of a library called django-environ. We can see the formats expected by DATABASE_URL at https://github.com/jacobian/dj-database-url#url-schema.

5 - Use the django-extension's sqlcreate management command to help to create the database

On Linux:

python manage.py sqlcreate | sudo -u postgres psql -U postgres

On Mac:

python manage.py sqlcreate | psql

On Windows:

Since there is no official support for PostgreSQL 12 on Windows 10 (officially PostgreSQL 12 is only supported on Windows Server), we choose to use SQLite3 on Windows

6 - Run the migrations to finish configuring the database to able to run the project

python manage.py migrate
coverage run -m pytest

If for some reason you get an error similar to bellow, is because the DATABASE_URL is configured to postgres:///everycheese and because of it the generated DATABASES settings are configured to connect on PostgreSQL using the socket mode. In that case, you must create the database manually because the sqlcreate is not capable to correctly generate the SQL query in this case.

ERROR:  syntax error at or near "WITH"
LINE 1: CREATE USER  WITH ENCRYPTED PASSWORD '' CREATEDB;
                     ^
ERROR:  zero-length delimited identifier at or near """"
LINE 1: CREATE DATABASE everycheese WITH ENCODING 'UTF-8' OWNER "";
                                                             ^
ERROR:  syntax error at or near ";"
LINE 1: GRANT ALL PRIVILEGES ON DATABASE everycheese TO ;
ERROR:  role "myuser" already exists
ERROR:  database "everycheese" already exists
GRANT

You can delete the database and the user with the commands below and then perform step 5 again.

⚠️ Be very careful here!: The commands below erase data, and should only be executed on your local development machine and NEVER on a production server.

On Linux:

sudo -u postgres dropdb -U postgres --if-exists everycheese
sudo -u postgres dropuser -U postgres --if-exists myuser

On Mac:

dropdb --if-exists everycheese
dropuser --if-exists myuser