chevron_left
0 points
5

  1. Introduction

New to Django? You have just learnt about PyCharm and want to get your hands dirty with some real work? Couldn’t find the right guide or tutor? Don’t you worry, this article just came to your rescue. The article you’re reading now will walk you through the steps needed to create a Django project using PyCharm IDE Community Edition.
It’s not going to take too much of your time, but it promises to deliver a very effective, efficient and wholesome guide.


    2. How To Create a Django Project in PyCharm
    a. Pre-requisites

In order to get the best out of this article, make sure you have both Python and PyCharm Community edition installed on your machine. (It is strongly recommended to use Python version 3). To save you the time, here is the PyCharm download link: https://www.jetbrains.com/pycharm/download
I’m going to use PyCharm version 2020.3.2. Don’t worry too much about having the exact same version of PyCharm though, as this article should stay valid for any version (UI differences between different versions should not be so confusing).

    b. Let’s Create a NEW PROJECT!

● Everything has a starting point!
After you start PyCharm, you will be prompted to either create a new project or open an existing one. You guessed right, let’s choose New Project


image

Now, you can notice that we can type the project name as well as the location of the environment. It is a good practice to have a specific development environment for each of your Python projects instead of having your packages installed globally (this can cause conflicts e.g: installing different versions of the same package globally). The good news is: Pycharm does this for us! Another good practice is to give the virtual environment the same name as the project. There are many fancy and cool names we can choose, let’s keep it simple though and go with simple-django, that’s so simple right?

image


We will now do a simple verification: We will check whether the virtual environment got successfully created and automatically activated or not. To do that, let’s open up the terminal window in PyCharm. Make sure that the name postfixed to the prompt is the same as the name we have chosen earlier. Don’t worry about leaving the checkboxes at their default state, since we will only create a really basic project and we won’t need any of the intricacies that they might offer. Finally, click on Create

 

image



● We all love installing new things, don’t we?
I hope you didn’t have any problems thus far because things will be getting real now! We are going to finally install Django using PyPI (Python Package Installer). Let’s open the PyCharm terminal and write the following command:

pip install "Django"


image

 

Remember the virtual environment we created earlier? The previous command will add the Django package to that environment so that we can kickstart our Django app!

● Let’s start a new Django project.
By typing the following command in the terminal you will make a big step towards creating your first Django project:

django-admin startproject mysite 

 

image
You will notice now that there are a bunch of new files created for you by the Django manager. You will also see the project lurking inside the ‘mysite’ subdirectory. 

 

image



● We will migrate now!
Since Django abstracts data into the Model layer, most developers rarely need to directly interact with the underlying database.
To apply the database migrations, let’s run the following command, make sure you’re located in the mysite directory:

python manage.py migrate 

image

● Let’s press the launch button!
We are all set! Let’s finally press the launch button and see our glorious new web app in action. To do this, we simply need to launch the embedded development server, and we do this by running the following command: 

python manage.py runserver 

Open the following URL in your browser to see your app live on the web: : http://127.0.0.1:8000/ 

image



● New app on the way!
Once our project is ready, it’s time to create our own app. This is as simple as the previous step, we will first make sure we are located in the mysite directory then we can type this command: 

python manage.py startapp myapp 

image

 

Wow, did you see that? There’s a new directory called myapp that was just created. It also contains some new files that were automatically generated. Feel free to investigate those files. 


● Where is my App? 

After the creation of the myapp Django application, we just need to register it in our mysite Django project. Open the Django project’s settings.py file and locate the INSTALLED_APPS variable. Let’s head to the settings.py file and go to the INSTALLED_APPS variable where we will append the ‘myapp’ at the end of the array. Now we are all set, hit save and continue. 

image

● Let the first page see the light! 
Let’s open the views.py file. This is one of the files that were created for us by the Django project manager. You can find it inside the myapp subdirectory.
You will now put your magic fingers on your keyboard and try to make this file look like the one in the screenshot below: 

 

image

Right-click on the myapp subdirectory inside Project Manager and click on New -> Python File from the menu. Just specify the name as urls.py,
image


Now add this content to the file: 

image

Let’s go to the urls.py file that was automatically created for us (not the one that we’ve just created). There’s a variable called ‘urlpatterns’. Simply add the URL for the myapp application at the end. Also, make sure you import the include function from django.urls

image

    3. Conclusion
Well done! You’ve just taught yourself the most essential thing to build your next amazing apps! PyCharm just made your life 100 times easier as it did to many millions of developers around the world! Be proud of yourself, continue on your learning journey and dive deeper into the world of website creation, I promise you won’t regret it!

More Posts

Develop a shopping cart website with django 2 and python 3. Tania - Sep 13
Django error that port is already in use amna - May 30
Which framework should you use: Django or Laravel? Lelin - Apr 24
Python ecommerce | build a django ecommerce web application. Tania - Sep 14
Creating and Understanding a Django project mohammed - Aug 9
Introduction to Django as a Framework mohammed - Aug 8
Number of occurrences of a character in a string in python with example. Tania - Sep 13
flask vs django differences Kushan Gayantha - Aug 9
Basic Django Project mohammed - Aug 8
Setting up Python3 and Django for Linux / MacOS / Windows / Android mohammed - Aug 8