Django Intro
How To Build Your First App In Python Django Framework — DjangoSeries Episode # 00
Hi, we will make our first app in Django using anaconda environment in Atom in ten steps.
Conda is a tool for managing and deploying applications, environments and packages.
And how about Django?
Django is a free and open source web framework.
It is used by many sites, including Pinterest, Instagram, Bitbucket, Washington Times, Mozilla and more!
A Django application is a collections and configurations that when combined together will make up the full web application (your complete website running Django).
Each application is created (9th step)
to perform a particular functionality (Registration, Polling, Comments, etc.)
We will use Atom as editor.
Let’s prepare our system.
Download Atom
Open Atom and install Atom Django (optional):
Install autocomplete-python (optional):
Install “autocomplete-python” package in Atom. { Welcome Guide -> Install a Package -> Open Installer -> Install -> (Search) autocomplete-python -> Install }
Install “platformio-ide-terminal” package in Atom. { Welcome Guide -> Install a Package -> Open Installer -> Install -> (Search) platformio-ide-terminal -> Install }
Then, Open Platformio-Ide-Terminal Settings { Welcome Guide -> Install a Package -> Open Installer -> Packages -> platformio-ide-terminal -> settings }.
🔑 Change Shell Override to “C:\WINDOWS\system32\cmd.exe” . {platformio-ide-terminal -> settings -> Core -> Shell Override}
👉 Download Anaconda Individual Edition and install it.
Now what matters: How to use Conda environments to code Django:
00#Step —Let’s activate Conda environment at Atom Terminal;
In the bottom left of Atom interface, click the plus sign, let’s run the terminal:
On Atom terminal, we need to Go To C:\Users\profile\anaconda3\Scripts\ and Run activate.bat Script, like this:
C:\Users\profile\anaconda3\Scripts\activate.bat(base) C:\Users\giljr\Documents\django_projects>
This will activate our Conda environment (prefixed with base).
Do not forget to override PowerShell terminal in platformio as key above 👆
Now we are ready to go!
01#Step — Make a Django Project Directory on Win Explorer:
cd C:\Users\profile\documents
mkdir django_projects
02#Step — On Terminal, searching for dev environments:
conda info --envs
That will brings our environments (just the base, so far…):
# conda environments:
#
base * C:\Users\giljr\anaconda3
03#Step — Go To Atom, open folder and point to your newly created directory:
04#Step — For Creating Django Conda Env, Type:
conda create --name myJangoEnv django
The following packages will be downloaded:
package | build
---------------------------|-----------------
asgiref-3.4.1 | pyhd3eb1b0_0 25 KB
ca-certificates-2021.7.5 | haa95532_1 113 KB
certifi-2021.5.30 | py39haa95532_0 140 KB
django-3.2.5 | pyhd3eb1b0_0 3.1 MB
libpq-12.2 | hb652d5d_1 2.7 MB
pip-21.1.3 | py39haa95532_0 1.8 MB
psycopg2-2.8.6 | py39hcd4344a_1 156 KB
python-3.9.5 | h6244533_3 16.5 MB
setuptools-52.0.0 | py39haa95532_0 725 KB
sqlite-3.36.0 | h2bbff1b_0 780 KB
sqlparse-0.4.1 | py_0 35 KB
typing_extensions-3.10.0.0 | pyh06a4308_0 27 KBThe following NEW packages will be INSTALLED:asgiref pkgs/main/noarch::asgiref-3.4.1-pyhd3eb1b0_0
ca-certificates pkgs/main/win-64::ca-certificates-2021.7.5-haa95532_1
certifi pkgs/main/win-64::certifi-2021.5.30-py39haa95532_0
django pkgs/main/noarch::django-3.2.5-pyhd3eb1b0_0
libpq pkgs/main/win-64::libpq-12.2-hb652d5d_1
openssl pkgs/main/win-64::openssl-1.1.1k-h2bbff1b_0
pip pkgs/main/win-64::pip-21.1.3-py39haa95532_0
psycopg2 pkgs/main/win-64::psycopg2-2.8.6-py39hcd4344a_1
python pkgs/main/win-64::python-3.9.5-h6244533_3
pytz pkgs/main/noarch::pytz-2021.1-pyhd3eb1b0_0
setuptools pkgs/main/win-64::setuptools-52.0.0-py39haa95532_0Proceed ([y]/n)? yDownloading and Extracting Packages
pip-21.1.3 | 1.8 MB | ####################### | 100%
certifi-2021.5.30 | 140 KB | ####################### | 100%
tzdata-2021a | 108 KB | ####################### | 100%
sqlparse-0.4.1 | 35 KB | ####################### | 100%
wincertstore-0.2 | 15 KB | ####################### | 100%
django-3.2.5 | 3.1 MB | ####################### | 100%
typing_extensions-3. | 27 KB | ####################### | 100%
libpq-12.2 | 2.7 MB | ####################### | 100%
python-3.9.5 | 16.5 MB | ####################### | 100%
sqlite-3.36.0 | 780 KB | ####################### | 100%
asgiref-3.4.1 | 25 KB | ####################### | 100%Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate myDjangoEnv
#
# To deactivate an active environment, use
#
# $ conda activate myDjangoEnv
#
# To deactivate an active environment, use
#
# $ conda deactivate
04#Step — See if everything is all right:
conda info --envs
You will see:
# Conda environments:
#
base * C:\Users\giljr\anaconda3
myDjangoEnv C:\Users\giljr\anaconda3\envs\myDjangoEnv
05#Step — Activating your newly created Django environment:
conda activate myDjangoEnv
This results:
(myDjangoEnv) C:\Users\giljr\Documents\django_projects>
06#Step — Starting our Django Project:
django-admin startproject first_project
This will create a new first-project directory, like this:
07#Step — Lets use manage.py now:
Change to first_project directory and type on terminal:
python manage.py runserver
Here is the output:
(myDjangoEnv) C:\Users\giljr\Documents\django_projects\first_project>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.
July 24, 2021 - 12:27:39
Django version 3.2.5, using settings 'first_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[24/Jul/2021 12:27:42] "GET / HTTP/1.1" 200 10697
[24/Jul/2021 12:27:42] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[24/Jul/2021 12:27:42] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[24/Jul/2021 12:27:42] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
[24/Jul/2021 12:27:42] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
Not Found: /favicon.ico
[24/Jul/2021 12:27:42] "GET /favicon.ico HTTP/1.1" 404 2117
08#Step — Go To http://127.0.0.1:8000/
There you have it!
Now let’s talk about Migrations and Django Application.
As you can see (output above) there is a warning about 18 unapplied migrations.
Migration allows you to move databases from one design to another, this is also reversible.
So you can migrate your database.
For now you can ignore this warning.
Let’s continue and create our first Hello World Django Application.
Inside your project folder you can have any number of application. Awesome, right?
Let’s get some terminology straight about Django:
09#Step — Let’s create our simple first app with this script:
Ctrl+c to stop the server, and type:
python manage.py startapp first_app
As you can see it will create others directories inside first_project directory.
Let’s see what does this files mean:
10#Step — Now let’s learn the process of creating a view and mapping it to a URL!
Go To your Atom and open the first_project settings.py file, and type:
(...)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app'
]
This will registrate your app inside your first project.
Now open first_app views.py and type:
from django.shortcuts import render
from django.http import HttpResponse# Create your views here.def index(request):
return HttpResponse('Hello World! This is my First Django Page! Welcome!!!')
Each view must return a HttpResponse return.
Now map this view in the the fisrt project urls.py file:
from django.contrib import admin
from django.urls import pathfrom first_app import viewsurlpatterns = [
path('index/', views.index, name='index'),
path('admin/', admin.site.urls),
]
Finally just call the server again:)
python manage.py runserver
And Go To your browser and point it to:
http://127.0.0.1:8000/index/
And here is the page:
Now, we have django project and 1 app running beautifully!
That’s all for today!
See you soon at the next Django Episode o/
Now, Reminders:0 - django-admin startproject first_project
python manage.py startapp first_app1 - Register your app in first_project settings.py at INSTALLED_APPS list;.2 - From first_app views.py create a method to handle the request:
from django.http import HtttpResponse # Create your views here. def method_name(request):
return HtttpResponse('Your_Message_Goes_Here')3 - Map the view to first_project urls.py:
from first_app import views
urlpatterns = [
path('method_name/', views.method_name),
path('admin/', admin.site.urls),
]4 - python manage.py runserver
Related Posts
00#Episode — DjangoSerie — Django Intro — How To Build your First App in Python Django Framework — DjangoSeries (this one)
01#Episode — DjangoSerie — Django MTV In VS Code — How To Install Django Inside Virtual VS Code
02#Episode — DjangoSerie — Can You Solve This in Python? — Here is A Basic Python Question!
03#Episode — DjangoSerie — JUNGLE-DJANGO Webpage! This Is My New Django Netflix Clone Page!
04#Episode — DjangoSerie — A Django Blog In VS Code — Quick Start! — Part_I
05#Episode — DjangoSerie — A Django Blog In VS Code — Database, Migrations & Queries — Part_II
06#Episode — DjangoSerie — A Django Blog In VS Code — Bootstrap, Tailwind CSS — Part_III
07#Episode — DjangoSerie — A Django Blog In VS Code — Forms & Validations — Part_IV
08#Episode — DjangoSerie — A Django Blog In VS Code — Login & Logout — Part_V
09#Episode — DjangoSerie — A Django Blog In VS Code — Upload Profile Picture — Part_VI
10#Episode — DjangoSerie — A Django Blog In VS Code — Update & Resize Picture — Part_VII
11#Episode — DjangoSerie — A Django Blog In VS Code — Class-Based-View & CRUD — Part_VIII
12#Episode — DjangoSerie — A Django Blog In VS Code — Posts Pagination & Quick DB Population — Part_IX
13#Episode — DjangoSerie — A Django Blog In VS Code — Self-Service Django Password Reset — Part_X
14#Episode — DjangoSerie — A Django Blog In VS Code — Heroku Deploy — How To Push Your Site To Productio — Part_XI
Sharing is caring.
by me
Credits & References
Python and Django Full Stack Web Developer Bootcamp! by Jose Portilla
Introduction to ASGI: Emergence of an Async Python Web Ecosystem by Florimond Manca
Is Django still good?No. It’s better and in many cases it’s probably the best.Django is a fantastic framework with thousands of plugins aka apps and has a huge developer community. Whenever you find a problem, you’ll get a solution from SO, blogs and other resources. You don’t like the way Django handles a particular feature? No problem, you can customize each and everything in a very natural way.It was very well designed as a result core concept of the framework didn’t change over time. So if you’ve learned Django 1.1 a long time ago, you’ll be able to start with 1.11 very easily.from https://www.quora.com/ by Nayan
Another good articles by Phillip Oldham
What is Django good at that Flask isn't?Django is good at being able to quickly bootstrap a project. It is a “batteries included” framework: the built-in code generation tooling, database library, templating system, admin website, and testing system help you get a project started easily and lay down the basics very quickly… to the point where you hardly write any code to have a fully working initial system. Django has a powerful plugin system and a plethora of great plugins. Developers can quickly build complex systems without having to get too deep into the inner workings.However, these features can also be problems. If your project doesn’t fit within Django’s scaffolding, with the way Django defines how things should be built, what tooling should be used, you can find yourself impeded by it. It can feel a little bit “paint by numbers”: you generate an app, fill in the blanks, and something is working… but maybe not quite the way you wanted it to. Similarly with plugins; you must follow their guidelines to make them work the way they have been designed. If you need to veer out of their defined way of working, you’re gonna have a bad time of things.Flask is “bare bones”, focusing only on the “web” part of the stack: requests, routing, and responses. You have to do more up-front to get things working, but you’re in control of everything. No “filling in the blanks” with Flask; you’re writing raw Python to do exactly what you need to do — no more, no less. You don’t have to use the prescribed database library, templating system, testing tools… you not even required to have them. Every choice is yours.Flask also has a plugin system, though with fewer plugins than Django. However, those plugins are a lot more focussed on fleshing out the features of Flask itself, rather than adding new features that are already available from other libraries. These plugins help move Flask closer to Django’s “batteries included” model, but with one key difference: you can include only the ones you want/need.Both frameworks have their place, their advantages. Developers should try to avoid the… “religious” undercurrents of our craft, and focus on using the right tool for the job. I personally prefer Flask to Django, as I prefer working in a more function-focussed style with Python (modules, functions, and composition rather classes, hierarchies, and inheritance). However, I would never discount Django as an option without reviewing requirements of the project at hand.from https://www.quora.com/ by Phillip Oldham
Our greatest glory is not in never falling, but in rising every time we fall.