This post will be a series of posts that will enable us to build a fully functioning ecommerce website named ESHOP using django. For you to effectively understand this, you will need to at least have a basic understanding of python and object oriented programming as well as a basic understanding of html and css, and basic JavaScript. You can follow along if you are still new to django and I will try to comment as much as I can to make the code more understandable. I have tried to make the code as much pythonic as I can but there will be some rules broken for one or two reasons.
In case there is some error that you find, or you code doesn't run as expected, you can always reach out to me on my email, which I will leave at the end of this tutorial. Follow along and Happy coding!!
Prerequisites
- Code Editor (Any code editor that supports python can work but for our purposes, we will use sublime text which can be downloaded from sublimetext.com)
- Python 3.9
- Django 3.2
- Virtualenv
- Postgresql
- git(Not neccessary for you but you will need to have git knowledge for your own projects) Check out git-scm.com to download and familiarise yourself with git
Setting up the project - Download Python 3.9 from the python.org website and install it. Ensure that you have added python to
PATH
.For a more detailed step by step process check out how to install python and add it to path - Fire up the command prompt and type
pip install virtualenvwrapper
This command will install a virtual environment to your machine. We use virtual environments to separate project dependencies and ensure that they do not conflict with subsequent projects dependencies. - You can create a new virtualenvironment by typing
mkvirtualenv eshop
on the command prompt. - Note the name of the virtual environment and dont forget it.
- Every time you need to activate the virtual environment on windows, you can open up the command prompt and type
Envs\eshop\Scripts\activate
and the environment will be activated. On ubuntu, you can open up the terminal and typesource eshop\bin\activate
and the virtual environment will be activated. - The next step is to download the dependencies. This step will be constant for all the dependencies that we will need for this project.
- The first dependency we will need is django. You can download the latest version by simply typing
pip install django
or a specific version by typingpip install django==3.2
- Wait until django and its dependencies are installed.
- After the installation is complete, you can check for the version installed by typing
python
on the command prompt and then on the python interactive shell created, type
>>> import django
>>> print(django.get_version())
3.2
- To exit the shell type
exit()
- Start a django project named eshop by typing
django-admin startproject eshop
- This command will create a directory named
eshop
in your current working directory and the directory will have this structureeshop/ |- manage.py |- eshop/ |- urls.py_____________________ # urls configuration file |- settings.py_____________________ # Website configuration file |- __init__.py _____________________ # Empty file to enable python to read this folder as a package |- wsgi.py |- asgi.py
- The next step is to get into the root
eshop
directory using the following commandcd eshop
- After that run the following command
python manage.py runserver
to test that the installation worked correctly. The expected output is similar to
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.
April 24, 2021 - 15:05:18
Django version 3.2, using settings 'eshop.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
- If you encounter this error
python: can't open file 'manage.py': [Errno 2] No such file or directory
it means that you are not in the right directory and you should get into the rooteshop
directory as instructed above. - Fire up your preferred browser and type
http://127.0.0.1:8000/
orlocalhost:8000
- If you see the following screen, Congratulations!! It means that your setup has been done correctly and you can follow the steps that follow.