simple django app code example

Example 1: django create app command

python manage.py startapp app_name

Example 2: django create app

python manage.py startapp # name of the app

Example 3: how to start a new django project

...\> django-admin startproject project_name

Example 4: django basic steps

# --------- Basic steps for working with Django on a MAC ---------- #

 1º - Create a virtual environment by giving this command:
  
 >> python3 -m venv "name_of_env"  
  
  
 2º - Activate this virtual environment:

 >> source "name_of_env"/bin/activate  


 3º - Now you can install Django:
  
 >> pip3 install django  
  
  
 4º - To initiate a project in Django:

 >> django-admin startproject "projectName"
 >> cd projectName
 

 5º - To run the server, you need to go to the directory containing 
      manage.py and from there enter the command:
  
 >> python3 manage.py runserver

 
 6º - To create a basic app in your Django project you need to go 
      to the directory containing manage.py and from there enter 
      the command:

 >> python3 manage.py startapp "projectAppName"


 7º - You need to include the app in our main project so that 
      urls redirected to that app can be rendered. Move to 
      "projectName" -> "projectName" -> urls.py and change the
      following:
    
    
from django.contrib import admin 
from django.urls import path, include 
  
urlpatterns = [ 
    path('admin/', admin.site.urls), 
    # Enter the app name in following syntax for this to work 
    path('', include("projectApp.urls")), 
]

Example 5: simple django app

# start by installing django by typing the following command

python -m pip install Django

# you can check if it worked by typing

python3 -m django --version # for mac

python -m django --version # for windows

# when that works you can start your project by

django-admin startproject your_project_name

# then you can start your first app by

django-admin startapp your_app_name

# before you can run it, you need to add the app to the project

# go to the folder your_project_name and open settings.py and scroll down until you see this

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

# then go to the folder your_app_name and open apps.py and copy the class name

# when you did that, add the following line to installed apps

'your_app_name.apps.the_class_name_you_just_copied'


# happy coding!

Example 6: django make app

...\> django-admin startproject mysite