In this quick tutorial I will show you an example of the process I follow to setup and run a Django project in a matter of few minutes. When writing this tutorial, the version of python in my mac book was 3.11.0.
To see the version of default Python in your mac, type:
$ python --version Python 3.11.0
To know the version of latest Python3 type:
$ python3 --version Python 3.11.0
(Note: You may have different python
and python3
versions in your Mac. If they are different use python3
and pip3
in the commands accordingly to use latest python in your project. If they are same you can simply use python
and pip
while running commands.)
Install pipenv
In our project we will use pip
to manage python packages. pip
the package manager for python
(and pip3
for python3
). It is similar to that of composer
to manage PHP packages.
Install pipenv
by typing
pip3 install pipenv
Install Django
Go to the directory you want to setup your first project in. It can be anywhere in your machine. Create your project directory and install Django in it.
cd ~/Desktop mkdir myproject cd myproject pipenv install django
Creating a virtualenv for this project... Pipfile: /Users/inimist/Dropbox/Python/storefront/Pipfile Using default python from /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 (3.11.0) to create virtualenv... ⠹ Creating virtual environment...created virtual environment CPython3.11.0.final.0-64 in 908ms creator CPython3Posix(dest=/Users/inimist/.local/share/virtualenvs/storefront-v5OoWkqu, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/inimist/Library/Application Support/virtualenv) added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.1 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator ✔ Successfully created virtual environment! Virtualenv location: /Users/inimist/.local/share/virtualenvs/storefront-v5OoWkqu Creating a Pipfile for this project... Installing django... Resolving django... Added django to Pipfile's [packages] ... ✔ Installation Succeeded Pipfile.lock not found, creating... Locking [packages] dependencies... Building requirements... Resolving dependencies... ✔ Success!
Now you should have two dependency manager files in your myproject
folder, as shown below:
myproject inimist$ ls Pipfile Pipfile.lock
Next, start the virtual environment using
pipenv shell
The above command will start a virtual environment using pipenv
in your current directory. If setup correctly it should add (myproject)
to your cli console.
Create new project
Once we have installed Django and our virtual environment is on we can use django-admin utility to create our first Django project application.
django-admin startproject myproject .
If you omit adding the dot(.
) at the end of above command, it will create another myproject folder inside the current myproject folder. It ends up having three myproject folders in the project tree. So having a dot(.) is crucial which instructs the django-admin
utility to create project in the current folder and not to create another folder for it.
So now since we have created our new Django project and an application within it is the time to run it.
python manage.py runserver
Result:
(myproject) bash-3.2$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 27, 2023 - 06:07:33
Django version 4.2.4, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Note: Django runs on port 8000 by default but you can use different port when running runserver command.
$ python manage.py runserver 9000
In the result of the command above it you can see some message is in red color. This is because we haven’t run the migrations yet. So we can run migrations using:
$ python manage.py makemigrations $ python manage.py migrate
Now you can run the following address in the browser:
http://127.0.0.1:8000/
Which should show the following in the browser
Conclusion
You can setup Django with help of venv or pipenv in Unix based systems. You should install PIP to install python dependencies and Django. Once setup you can run migrations and run your first django project in browser. The default port used by Django is 8000 but you can change it using it as a last parameger while running the runserver command.