Step by Step Guide on how to set up a Django Database
Tags: django, python
Published: 2020-07-20 23:23:43

For my Capstone project with Flatiron Bootcamp one of the requirements was learning a new technology. Since I have an affinity for backend data I wanted to take a stab at Django. While researching I noticed a lack of step-by-step guides and thought I'd write one!

To start off you will need to download django and pip(which is a package management system for django). If you are a Mac user, like myself, you will already have python installed. Make sure you have the most recent version of python before starting your project! 

Additionally, you will need to install pip3. Run: install pip3.

Once you have confirmed you have the most recent version of python and pip, create a directory for your project. Once in your project, there are several commands you will need to execute. 

You will need to first create a venv, which stands for virtual environment. This ensures that any changes made to your website won't affect other websites you're developing. You will want to create the venv with the most recent version of pip, pip3. Run: pip3 install virtualenv.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vggz5svdwem13qd64z5i.png)

Once completed, I ran an additional code snippet to change the name python3 to just python in the venv. When running commands you must have python included prior to your command. You will see what I'm talking about later in this guide. Run: virtualenv --python=python3 venv.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x8wszn8hdwlkwy5isny3.png)

After creating your venv and renaming python3 to python, you will need to activate your venv. When it's installed, it includes a bin folder that you need to activate. Run: source venv/bin/activate. Once activated, it should look like this, where it has (venv) before the command line:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f532lfvrbqbf8vcvx67t.png)

Next, you will want to install Django in your venv. Run: pip install django.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uvv4jymfkrgv3vavnw5e.png)

Then install the django restframework:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hr5ywfpiu5hhjkr0i3in.png)

Once these steps are completed, you can create your project folder. Run: django-admin startproject [your_project_name].

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/afp7m35cc8erbstw6jwv.png)

Your file structure should now look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gqc0v0aedbtxnusfovod.png)

cd into your new project folder and rename the folder src:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7fn1j747yxxceqg3f8hn.png)

cd into your src folder once it has been renamed.

------------------------------
WARNING/EDIT: If you are creating a Database with Users/Auth, you will need to do these steps, BEFORE migrating for the first time! If you have already gone too far, I will show how to remedy this in my Django Auth Blog.

Directly from Django Docs:
Due to limitations of Django’s dynamic dependency feature for swappable models, the model referenced by AUTH_USER_MODEL must be created in the first migration of its app (usually called 0001_initial); otherwise, you’ll have dependency issues.

You will need to put this in your Settings.py in your Project Folder. It should be AUTH_USER_MODEL = [your_app_name.user]:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/atikwev4bpkzjlteqfmz.png)

Additionally, add this to your Models(You can customize further later on, I will be going into detail in my Django Auth installment):
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e5rn8zmuly55le9v0euy.png)

Then, you will need to register your user model in your Admin.py:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/arakdcczqtwegg2iv2yf.png)
------------------------------

Ok...back to the standard set up....

Once you are in your project src folder, you will need to run: python manage.py migrate. This will migrate over all of the Django admin and user information that comes pre-packaged with Django!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iie4ye6cxn608zze7okh.png)

Then, create a super user- this will be your admin information for you to access and change your backend data on the web browser. Run: python manage.py createsuperuser. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6rij2izonly2uvqiyiy1.png)

It will prompt for a user, email and password. The email is optional.Once completed the terminal will say: SuperUser created successfully. 

To confirm, run: python manage.py runserver click on the localhost site provided in the terminal and login using the credentials you just made!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bez0vyxp07sa9y4lrx5w.png)


Finally, you can start your app! Run: python manage.py startapp [app_name]. This is where you will begin to use python manage.py at the beginning of each command you run. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s0iw4zncie66i7kfr2h9.png)

Your file structure should look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u6754wn7856pl4yxjjb8.png)

Install cors headers using: pip install django-cors-headers. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nf1xbbusomy0rm2q18t2.png)

Once your app is created, be sure to add your app name, rest framework and cors headers to the list of installed apps in settings.py.

Include cors headers information in your middleware in settings.py:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8hyl6nie9vjfzzq3bdh7.png)

Once that is completed the fundamentals of your django database are set up! This is part 1 of (at least) 2 blogs I will be creating to show how to set up and create your Django DB. 

EDIT/TIP: You may want to name your app folder with additional clarification such as: [app_name_app]. In order to differentiate between the project and app folders. You will always need to run commands from your project folder in order to access the manage.py file, so it helps to have a distinction between the two!

Happy Coding!