Git Product home page Git Product logo

django-example's Introduction

Learn how to integrate PlanetScale with a sample Django application

This sample application demonstrates how to connect to a PlanetScale MySQL database, create and run migrations, seed the database, and display the data.

For the full tutorial, see the Django PlanetScale documentation.

Set up the Django app

  1. Clone the starter Django application:
git clone [email protected]:planetscale/django-example.git
cd django-example
  1. Start the virtual environment
python3 -m venv env
source env/bin/activate

For Windows, use env/Scripts/activate.

  1. Install the required packages:
pip install -r ./requirements.txt

Set up the database

  1. Sign up for a free PlanetScale account and create a new database.

  2. Click the "Connect" button to generate credentials for database branch (main is default). Select "Django" from the language dropdown and copy the values in the sample .env file.

  3. Modify your .env file in your Django app with the values from the previous step:

DB_DATABASE=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=
MYSQL_ATTR_SSL_CA=

Note: The value for MYSQL_ATTR_SSL_CA may differ depending on your operating system.

  1. In the mysite/settings.py file, scroll down and look for the DATABASES object. Replace it with the following:
DATABASES = {
    'default': {
        'ENGINE': 'django_psdb_engine',
        'NAME': os.environ.get('DB_NAME'),
        'HOST': os.environ.get('DB_HOST'),
        'PORT': os.environ.get('DB_PORT'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'OPTIONS': {'ssl': {'ca': os.environ.get('MYSQL_ATTR_SSL_CA')}}
    }
}

Run migrations and seeder

  1. Run the migrations and seeder with:
python manage.py migrate

Start the application

  1. Start the server with:
python manage.py runserver
  1. Navigate to localhost:8000/products to see a list of data from the products table.

django-example's People

Contributors

hollylawly avatar nickvanw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.