Hi dev community!
This time I'll be showing you the basic process to develop a web app using Django. We'll be exploring most of its base functionality, seeing how its administrator view works, creating a database with foreign relations, and exposing it to your local network.
I'll try to make the process as easy to understand as I possible, so anyone interested in it, could create one.
Let's get start with the base of everything
What is Django?
Django is a python framework that allows you to develop web applications with the minimum effort. It provides a basic folder structure where you'll be placing logic functionality, HTML layouts, styles, and much more.
First things go first
Anaconda environments
Prepare an environment to install Django, an advantage offered by python is the availability to create environments with low memory usage, installing just the essentials, using Anaconda. Please install it by executing its Windows installer (in case you're using Windows) or any other installation method described in this page.
Installing Anaconda step by step is this post's out of reach, sorry :(
Once you have successfully installed Anaconda, then open up the command prompt and type the following command
conda create --name envname python=3.6
Please, change theenvname
to whatever name you want to set to your environment
What we have done is telling Anaconda to create a basic environment using python 3.6 version.
During installation, you'll be asked to confirm package and dependencies installation, so please type Y
to proceed without any troubles
Preparing Django
Great! time to install Django, inside your Anaconda prompt type the following command
conda activate envname
Please, change envname with your already set environment name
This will activate your environment whose going to be used
Now you're inside your environment, so time to install Django by typing the following
pip install Django
Note: Anaconda has a special way to install packages, that is supposed to be better than classic pip installation, however Django doesn't have an anaconda distribution.
Time to get dirty
Let's start validating our Django installation by running the following command inside Anaconda Prompt
python -m django --version
Remember to activate your environment with command described above
If your don't get an error saying No module names django
and get something like 3.x.x
instead , then django is ready!
Creating a project
Creating a Django project should be as easy as typing on your target directory
IMPORTANT change your default directory to something you remember well, because we'll be accessing to it further this series.
django-adming startproject project_name
Please, changeproject_name
to your website desire name
Then a directory should be created in the following structure
project_name /
manage.py
project_name /
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
```
1. The first **project_name** is the base folder where all project is located
2. **manage.py** is a python file that will let you interact with **Django** based in commands
3. The second **project_name** is a directory containing all necessary tools to work with **Django** storing database settings, urls, and configurations.
4. **__init__.py** an empty file to let know python this is a package.
5. **settings.py** a file with **Django** settings
6. **urls.py** your **Django** urls directory (when you create a page inside your website, your page url might be here)
7. **asgi.py** an entry point, allowing compatibility with ASGI servers (not used in this tutorial)
8. **wsgi.py** an entry point, allowing compatibility with WSGI servers (not used in this tutorial)
# Final thoughts #
At this point **Django** can run, just type this command on your project base directory (where `manage.py` is located). Easy, right?
> `python manage.py runserver`
And your done with a basic **Django** application. Surely you'll see a really basic webpage with almost nothing, but the adjustments and details will be tackle in the next post **;)**
Top comments (0)