Creating a social media web app: Django for Beginners

Ian Shin
6 min readOct 19, 2020

--

Django is a powerful web-framework written in Python that streamlines the hassle that comes with “traditional” web development. It is maintainable, scalable, allows for rapid development, and it’s easy to learn! There are many famous platforms that uses/used Django framework such as Instagram, Reddit, and even NASA!

Content

Creating a Virtual Environment.

Creating your first Django Project.

Setting up your first Django Project.

Creating a Virtual Environment

What is a virtual environment, and why do we need it?

When learning concepts in the field of computer science, I think the best way to learn is to use real life analogies!

Imagine opening a recipe book. Each page has its own requirements, directions, and steps to make a particular food right? There is no overlay with food recipes, and if I want a recipe for cake, I can simply turn to that page and start baking!

Those pages can be paralleled to the characteristics of a virtual environment! Virtual environments allow for the separation of required dependencies (ex. packages, versions), and only applies those dependencies in the environment where it was installed! So if I set up a virtual environment that runs on Python 3, with Numpy, TensorFlow, and Scikit-Learn installed… no other projects outside of this environment will be effected!

Let’s make a virtual environment!

Go to your search bar (bottom left hand corner),search for “cmd” , and open your command prompt! *If you want to learn more basic command prompt commands, click this link* All you need to get started is to have Python downloaded, as well as the Python’s package manager “pip”. (If you do not have pip installed simply follow the instruction listed on this website).

The next step is to select a path where you want your virtual environment to exist. A path is the “address” of a unique location, located on your computer. I would like my virtual environment to follow the path of..

User -> Documents -> MediumArticle

We can do this by using this the “cd”(change directory) command in the command prompt.

We use “cd” to move to a desired location where our virtual environment will exist. “User\Ian_S\Documents\MediumArticle” is my path to the virtual environment.

Awesome! Now we have a location where we can start our virtual environment. Run this following command (below) into the command prompt, and watch the magic begin!

python -m venv env

It may not look like much, but if you paste the path into your file explorer you should find a folder name “env”, and inside that folders you should see more folders/files!

We created our virtual environment, so lets activate it now! Type this command into your command prompt.

env\Scripts\activate

Notice that the last line has (env) next to it! This means that your environment is activated!

If you want to deactivate your virtual environment just type “deactivate” into the command prompt to do so!

Starting a Django Project

Let’s now start a Django Project in our new, activated virtual environment. First we have to “pip install django”… type this in your activated virtual environment!

You can ignore the warning I got. This is simply because I did not upgrade my “pip” to the latest version. It will not impact my outcome.

We can double check if Django has been correctly installed by running

django-admin- -version”. If downloaded properly, it should display your current Django version.

“django-admin.py” is now depreciated… use “django-admin — version” instead.

Now we get to create our Django project. Type the follow in your activated virtual environment.

django-admin startproject MediumProject

You can change “MediumProject” to whatever you please! This will serve as your project name.
In your file explore, you should be able to see these files!

Congratulations! You have successfully created your first Django project!

Setting up your Django Project

There are a series of steps we need to take. Let’s first run a Django’s development server. First we need to change our directory to our project name, and list all the existing files in that folder! Type these following commands into your activated virtual environment. *Remember if you changed your project name, simply replace “MediumProject” with the project name you listed*

cd MediumProject

dir (this will list all existing folders in the current directory)

You can learn more about the existing files, by looking at Django’s documentation. However, I would like to highlight the “manage.py” file because it is more likely you will be interacting with this file more than other.

“manage.py” will be used in command prompt, and allows the user to execute Django specific tasks. You will use it for running your deployment server, creating/executing migrations, running tests, etc… You will see more of “manage.py” in practice in future articles

Let’s try to run a deployment server to see if our project works! Run this line of code inside your project directory!

python manage.py runserver

Awesome! Our development server is up and running. You can simply copy the url(outlined in the white box), paste it into a web browser and check out your development server. You should see something similar to this.

If you are getting messages about “unapplied migrations” end the development server (press “ctrl +c” twice) enter the following..

python manage.py migrate

Re-run your development server! I will cover migrations in a future article.

Lets go on to making our super-user. Django has an awesome admin interface, which allows users with special permission to edit content that are present in the project.

Type this into the same place you ran your server.

python manage.py createsuperuser

*For the password, the text you enter will be invisible. Simply write out the full password and press “Enter”.

We can now access Django’s admin interface. Execute “python manage.py runserver” , copy the url for the deployment server, paste it into your web browser, and add “admin” to the end of it.

It should look similar to… http://127.0.0.1:8000/admin

Simply enter your credentials. You should be directed to the following image.

This administrative interface will be very useful in the future, especially when handling databases. We will dive deeper in a future article!

Congratulations! You created and finished setting up your Django project. In the next article, I will be diving deeper into the structure of Django, its inner-workings, and creating a homepage/registration for our Social Media Platform!

Thank you for reading!:D

--

--

Ian Shin

Full time CS/Econ nerd, part-time explorer, amateur cook.