Python for Everyone: Mastering Python the right way

am_eric

Eric Mutua

Posted on March 4, 2022

Python for Everyone: Mastering Python the right way

Well, your path to become a python guru is on track. Here we shall discuss on how to master python the right way.

THE BASICS!!

The basics of any programming language are fundamental in mastering the language as a whole.
Learning the fundamentals of python will give you the basic understanding of python. For starters, you can catch up with the basics of python programming here:
https://dev.to/am_eric/python-101-getting-started-1np2

INTERMEDIATE PYTHON

Next on track is intermediate python. Here you will get to understand data structures and algorithms and their implementation.Learn data structures and algorithms with me at
https://dev.to/am_eric/introduction-to-data-structures-and-algorithms-with-python-51fp.
Once equipped with these skills, you can now decide on your preferable track. It may be:

  • Web development

  • Data Science

  • Artificial Intelligence and Machine Learning

  • Game Development among others.

ADVANCED PYTHON

For web development, frameworks such as:

  • Django

  • Flask

  • Pyramid

  • Falcon

The most popular frameworks are Django and Flask.
Django - is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Flask - is a lightweight python framework that provides the tools necessary to create robust web applications.
Lets take a dive into Django.

Introduction to Django

Requirements:

  • You should at least have some knowledge in python programming before trying out a framework.
  • You should have python installed in your PC.

Installation

On your cmd on windows, do
pip install django

Wait till the installation is completed.
Check the django version installed. Do
py -m django --version

Next,create a virtual environment. Do
mkvirtualenv myapp

in an instance where it throws an error -

'mkvirtualenv' is not recognized as an internal or external command,
operable program or batch file.

Enter fullscreen mode Exit fullscreen mode

DO:
pip install virtualenvwrapper-win

then

mkvirtualenv myapp

Inside the virtual env:

(myapp) C:\Users\Eric>
Enter fullscreen mode Exit fullscreen mode

Do:
pip install django
to install django on the virtual environment

then
django-admin startproject myproject

To be:

(myapp) C:\Users\Eric>django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode

you can alter 'myproject' with the name of your preferred project.

Next, open the project on your preferred compilier.

Creating apps in myproject

Do:
python manage.py startapp myapp

(myapp) C:\Users\Eric>python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

Creating an app would look like this:

myapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
Enter fullscreen mode Exit fullscreen mode

Getting the First Output in Django

We use the views.py to return responses to the screen.
At the views.py, do:

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse(<h2>"Welcome to my first django application"</h2>)

Enter fullscreen mode Exit fullscreen mode

Next step is the ####URL-routing
Create a new python file and name it urls.py

myapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py
Enter fullscreen mode Exit fullscreen mode

Aturls.py:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
Enter fullscreen mode Exit fullscreen mode

Now run the project.

DO:
python manage.py runserver

The output will be;

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

March 01, 2022 - 15:50:53
Django version 4.0, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Enter fullscreen mode Exit fullscreen mode

Now that the server is running, visit http://127.0.0.1:8000/ at your web browser

Next,point the root URLconf at the myapp.urls module.
At myproject/urls.py , do;

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('myapp.urls')),
    path('admin/', admin.site.urls),
]
Enter fullscreen mode Exit fullscreen mode

The include() function allows referencing other URLconfs.

Finally, run the server again and refresh the page.

Do:
python manage.py runserver

The output 'Welcome to my first django application' should be visible from the page.

Thank You for taking time to read this article. Follow for more.

💖 💪 🙅 🚩
am_eric
Eric Mutua

Posted on March 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related