Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Set Up Your Venv, Django, and GeoDjango

It’s finally time to get started on our Django project and get to work in Python. We’ll start by creating a virtual environment, then install Django with Pip, and then enable GeoDjango.

Make sure you have the following items installed and working before you continue:

  • PostgreSQL with PostGIS
  • A shops database with PostGIS extension; and
  • Our geographic drivers, either installed on their own or as part of QGIS.

With that foundation in place you’re ready to start the web server portion of your project using the Django and GeoDjango frameworks.

00:00 Welcome to the fourth video in our series for building a location-based web application with Django and GeoDjango. It’s finally time to get started on our Django project and get to work in Python. We’ll start by creating a virtual environment, then install Django with pip, and then enable GeoDjango.

00:19 Make sure to have the following items installed and working before you continue: PostgreSQL with PostGIS, a shops database with the PostGIS extension, and our geographic drivers either installed on their own or as part of the QGIS application. With that foundation in place, you’re ready to start the web server portion of your project using the Django and GeoDjango frameworks.

00:45 Let’s start off by creating a new Python virtual environment. For full disclosure, I use PyCharm as my primary editor, and PyCharm automatically creates virtual environments for each new project. In addition, PyCharm provides point-and-click pip installations and templates for popular frameworks like Django. If you use PyCharm as your editor, I encourage you to take advantage of these time-saving features. In this video, however, I will be completing these tasks by hand at the command line to keep the instructions universal. First, I create a new folder for my project called geoshops.

01:24 You should create a new project or project folder with a similar name to follow along with the next steps.

01:36 Then, from within my new geoshops/ folder, I’m going to create my Python virtual environment and activate it with the following two commands. After activating the environment, notice the context of the command prompt reflects this.

01:53 The next task is to install Django using pip. Open the geoshops/ folder in your editor—in my case, PyCharm.

02:01 Notice your virtual environment’s subfolder there. Don’t make any changes to this. I will use PyCharm’s built-in terminal to install Django, and your editors should have a similar mechanism. But if it doesn’t, you can use the terminal or command prompt your operating system provides. Within the geoshops/ folder, type the command pip install django and press Enter.

02:26 The installation has completed successfully. The next step is to set up our Django project. While the geoshops/ folder is our way of organizing our work into a project folder, geoshops/ is not technically a project from Django’s point of view.

02:43 A project in Django is a functioning website. It’s now time to create this. Let’s call it shops to distinguish it from our geoshops/ folder.

02:53 Django provides us with a script to automate this process. At the command line, we will run the script django-admin.py followed by the startproject argument and the name of our project, shops.

03:08 Notice the period (.) at the end of the command. Without that period, the script would create two levels of shops/ folders—a shops/ folder within a second shops/ folder.

03:18 Since I’ve already created my geoshops/ projects folder, I will use the period to limit the creation to just one shops/ folder.

03:32 Once the script finishes, our Django shops website is created. If we refresh the contents of our folder, we notice we have a new file manage.py at the root of geoshops/, along with a subfolder named shops. Within the shops/ folder, we have four files.

03:48 Let’s take a brief moment to describe the purpose of manage.py, along with the new files in shops/.

03:56 manage.py is a script we will be using to perform administrative tasks for our new website. Now let’s look at the files in the shops/ folder.

04:05 __init__.py exists so your shops/ folder can be treated as a module. wsgi.py is there to help establish your site on a web server.

04:14 You will generally stay out of these two files. The other two files however—settings.py and urls.py—are where you’ll configure how your website operates.

04:25 settings.py contains key-value pair variables.

04:30 urls.py is there to route incoming requests to your site. You can think of it as a front desk reception for visitors to your website. We’re going to complete one more small task before ending this video.

04:44 Open settings.py and locate the key-value pair INSTALLED_APPS. Add the following string element to the end of the list: 'django.contrib.gis'. Great!

05:00 Your Django project is now GeoDjango-enabled. We’ve done a lot. This is a good spot to take a break before we wire up Django to use our Postgres database.

05:11 Take a moment to look inside of settings.py, and when you’re ready, I’ll see you in the next video.

Avatar image for Milan NIkolic

Milan NIkolic on May 1, 2019

Thank you Jackie, That was flawless. I truly enjoyed the course and finally signed up for RealPython. It was a matter of time. Just couldn’t resist finishing this series. After checking out the site, I am extremely happy I made that decision. Thanks and all the best to you and everybody at RealPython! M

Avatar image for Jackie Wilson

Jackie Wilson RP Team on May 1, 2019

I think that’s one of the nicest compliments I’ve received. Thank you so much Milan. Real Python is full of great stuff; I’m grateful and honored to be a part of it.

Avatar image for Milan NIkolic

Milan NIkolic on May 1, 2019

You are very welcome Jackie. I am looking forward to your future releases.

Avatar image for suneel0310

suneel0310 on May 9, 2019

Thanks in advance Jackie,

I am facing one issue that shops sub folder is not creating after enterning the command like django-admin.py startprojects shops .

Can you help me on that what i am missing.

Avatar image for Jackie Wilson

Jackie Wilson RP Team on May 9, 2019

Do you get any error when you try to run the script? Did your pip command correctly install Django? Also, are you on Windows?

Avatar image for suneel0310

suneel0310 on May 31, 2019

Thanks for your reply. I was busy with office work for the past 3 weeks, so i did not check your reply.

I was not getting any error when i ran the script. I hope pip command installed Django properly. yes i am on windows.

Let me check again by installing the Django andI will let you know if i am not getting again, please help me out.

Avatar image for hithard

hithard on Sept. 11, 2019

Hi Jackie,

I do not see shops directory being created after i run “django-admin.py startproject shops .” I just see the django-admin.py file open in pycharm when above command is run. django is installed as shown in previous step. even i reinstalled django but same issue. i m using win10 operating system. pls advice

Avatar image for Koldaman

Koldaman on Sept. 20, 2019

Hi Jackie,

Thanks for your very easy explanation and teaching methods. I am facing same thing @hithard mentioned.

Avatar image for Koldaman

Koldaman on Sept. 20, 2019

@ Hithard i founda way use this command line

django-admin startproject shops .

Avatar image for hithard

hithard on Sept. 22, 2019

@Koldaman it works. thanks a lot

Avatar image for Mahesh

Mahesh on April 12, 2020

While activating Source code im getting below error -

python -m venu myvenv – Created and next line i was giving below command source myvenv/bin/activate Error :Source is not recognized as an internal o0r external command,operable program or batch file PLs hellp

Avatar image for Mahesh

Mahesh on April 12, 2020

Above issue was in Windows …``

Avatar image for Ricky White

Ricky White RP Team on April 13, 2020

Hi maheshtpmail. That is the command for a UNIX OS. For Windows create you venv as before, then activate it with this instead:

myvenv\Scripts\activate.bat

If you are using Powershell instead of CMD use this one:

myvenv\Scripts\Activate.ps1
Avatar image for Mahesh

Mahesh on April 13, 2020

Thanks Rick - now its working — another problem im facing is after running below command django-admin.py startproject shops . it is creating only admin folder im not getting geoshops and shops folder

Avatar image for Ricky White

Ricky White RP Team on April 14, 2020

What does your folder structure look like?

Avatar image for Mahesh

Mahesh on April 17, 2020

Do we have any option to share screen(On your available time) and solve issue instead of waiting for a day to week and lossing time.

Avatar image for Ricky White

Ricky White RP Team on April 19, 2020

@maheshtpmail As you will see in the video you run django-admin startproject shops . inside the geoshops directory. Make sure you have created your geoshops directed first, then cd into the directory before your run the startproject command.

Avatar image for Mahesh

Mahesh on April 19, 2020

Thanks rick … Going through Django basic video - I will try again and update you… Little confusion with Mac installation commands and Windows Installation ....

Thanks for your quick support.

Avatar image for Mahesh

Mahesh on April 25, 2020

If we have screenshot feasibility it will be easy to send error –

Hey Rick issue now is after activating in Windows with below command myvenv\Scripts\activate.bat

(myvenv) C:\Users\Mahesh\geoshops1>charm . ‘charm’ is not recognized as an internal or external command, operable program or batch file.

Charm . comman is not recognizing and when i open pycharm directly from winodws geoshops1 folder and myvenv folder is not created in pycharm

Avatar image for Ricky White

Ricky White RP Team on April 26, 2020

Hi @maheshtpmail.

If you are to open Pycahrm from the terminal is then you need to type pycharm . not charm ..

Also you will need to tell Pycharm which interpreter you are using in the project settings. If you’re new to Pycharm, I’d recommend starting here: realpython.com/pycharm-guide/

Avatar image for Mahesh

Mahesh on April 27, 2020

Thanks Ricky - I will go though the link…

Avatar image for saadmanahmed00

saadmanahmed00 on Jan. 9, 2021

I am getting the following error while trying to make the nearby shop app. The error message: “django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal202”, “gdal201”, “gdal20”, “gdal111”, “gdal110”, “gdal19”). Is GDAL installed? If it is, try se tting GDAL_LIBRARY_PATH in your settings.” What am I doing wrong?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 11, 2021

@saadmanahmed00 You’re not doing anything wrong! GeoDjango requires a few additional open-source libraries, which need to be installed in your operating system. Please read Installing Geospatial libraries for more details.

Become a Member to join the conversation.