Python commands for Mac and Windows

python command notes

Python commands and VS Code Setup


To check Python version enter command 

Python3 – – version



To check with pip version

Pip3 —version


To check diesel version enter the command following

Django-admin —version


Install Django


We can install the Django in two types first we can install in virtual environment second we can install globally. In virtual environment if we install the Django it will not be accessible outside of this environment.


Install Django in Separate Environment


Install virtual environment wrapper

This is used to install virtual environment wrapper.

Pip install virtualenvwrapper-win


Example:-

(In windows ) pip install virtualenvwrapper 

(in MacOS) pip3 install virtualenvwrapper


Create virtual environment (VE)

This is used to create virtual environment. It will automatically activate environment.


For windows:-

mkvirtualenv envname


For example :-

mkvirtualenv ckesripyvrenv


For my macOS:-

Step 1. 

python3 -m venv envname

For example :- python3 -m venv ckesripyvrenv


Step 2

Source envname/bin/activate

For example :- source ckesripyvrenv/bin/activate


Step 3: If we want  to deactivate virtual env.

deactivate envname


Step4: remove env

rm -r bin include lib lib64 pyvenv.cfg share



Active VE

This is used to activate environment.

workon envname


For example :-

workon ckesripyvrenv


Installed Django in created VE

Pip install django

First activate environment then run the command to install django within active environment. You can also specify version pip install django == 2.0


Django Versions

django-admin —version



Create Project in django


Create Django Project

django-admin startproject projectname


Example

django-admin startproject schoolproject


Run Django Project

First goto the project root directory then run the below command

python manage.py runserver


optional command for, if we want to run project on specific port (for example 5555) then use below command

python manage.py runserver 5555


Stop Django Project

Press Ctrl+c



Create App in your django Project

To create app in your project, you have to move in your Project’s root directory. Root directory is the place where you manage.py application reside.


Create App

Py manage.py startapp appname


Example

py manage.py startapp studentApp

py manage.py startapp teacherApp

py manage.py startapp empleyeeApp


Create Model in App

Model is the place where you can define your fields and fields type. Like name, address, isStudent, dob,  etc. 

Where name & address as string, isStudent as Boolean, dob as date field.


Define Classes with Fields details

Open models.py file in studnetApp and following code.


class StudentModel(models.Model):

    SEX_CHOICES=(

        ('F','Female'),

        ('M','Male'),

        ('U','Unsure'),

    )

    fname=models.CharField(max_length=20)

    lname=models.CharField(max_length=20)

    rollNumber=models.IntegerField()

    dob=models.DateField(null=True)

    gender=models.CharField(

        max_length=1,

        choices=SEX_CHOICES

    )


Run make migrations command

By this command, we are telling Django that you’ve made change to your models and that you’d like the changes to be stored as a migration. 


To run migrations

python manage.py makemigrations appname

python manage.py makemigrations studentApp


To see migrations:

studentApp/migrations/0001_initial.py


To see ALTER table command:

python manage.py sqlmigrate studnetApp 0001


Create Tables in DB


Run migrate command to execute migration file,

By this command, execute the migration file to create table in database.

python manage.py migrate 



Insert Records


Step 1, Start shell

 python manage.py shell



Step 2, Import model

>>>from studentApp.models import StudentModel


Step 3, Insert recored

>>>s1=StudentModel(fname=‘Micky’,lname=‘Mouse’,rollNumber=2,dob=’1990-03-01’,gender=‘M’)

>>>s1.save()


Note: dob format ‘YYYY-MM-DD’


Step 4, new Inserted recored

>>>StudentModel.objects.all() // view summary

>>>StudentModel.objects.all().values() // view all fields with values


Uninstall Virtual environment wrapper

Step 1. Open command prompt

Step 2. Pip uninstall virtualenvwrapper



VS Code Plugin

  1. Django 
  2. Django Snippets
  3. Django Templates
  4. Prettier - Code formatter
  5. Pylance
  6. Python
  7. Python Debugger
  8. Black Formatter
  9. ESLint


VS Code Settings.json

{

  "window.zoomLevel": 2,

  "editor.minimap.enabled": false,

  "eslint.workingDirectories": [],

  "eslint.rules.customizations": [],

  "editor.formatOnSave": true,

  "editor.defaultFormatter": "esbenp.prettier-vscode",

  "emmet.triggerExpansionOnTab": true,

  "files.associations": {

    "*html": "html",

    "*.html": "html",

    "*.py": "python",

    "*.css": "css",

    "**/templates/*/*.html": "django-html",

    "**/templates/*.html": "django-html",

    "**/templates/**/*.html": "django-html",

    "**/templates/**/*": "django-txt",

    "**/requirements{/**,*}.{txt,in}": "pip-requirements"

  },

  "emmet.useInlineCompletions": true,

  "django.snippets.exclude": ["cms", "wagtail"],

  "emmet.includeLanguages": {

    "django-html": "html"

  },

  "[django-html]": {

    "editor.formatOnSave": true,

    "editor.quickSuggestions": {

      "other": true,

      "comments": true,

      "strings": true

    },

    "editor.defaultFormatter": "batisteo.vscode-django"

  },

  "[python]": {

    "diffEditor.ignoreTrimWhitespace": false,

    "editor.formatOnType": true,

    "editor.wordBasedSuggestions": "off",

    "editor.defaultFormatter": "ms-python.black-formatter",

    "editor.formatOnSave": true

  },

  "editor.codeActionsOnSave": {},

  "editor.indentSize": "tabSize"

}

Popular posts from this blog

CSS Mouse Cursors and Custom Cursors

Block right click or disable keyboard on web pages

What is PAD file? "Submit your software with PAD file to Download.com"