/Django

this is a django project, please feel free to fork, clone, and contribute, kurdi.89@homtail.com

Primary LanguageTcl

Django :

getting started with django (for windows):

download chocolaty :

open you CMD by right click and Run as administrator

copy and paste the following to download the windows package manager :

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

then to download python

run choco download python


for using another great command toll download Cmder from here, download it and exctract it to your Dev folder

run Cmder and to check if installed correctly run python --version


  1. set up project folders :

CD into a Dev folder , and then mkdir a new project folder and then CD into it

  1. pip install virtualenv

  2. virtualenv <venv>

  3. <venv>\Scripts\activate

  4. pip install django

  5. django-admin startproject <project_name>

if you want to run the project CD into it and then python manage.py runserver

  1. cd into the project folder and python manage.py migrate

  2. pip freeze > requirements.txt

  3. pip install -r requirements.txt

  4. notepad .gitignore then add the content to the gitginore file

  5. git init

if first time to use git : git config --global user.email "email@email.com" git config --global user.name "username"

  1. git add . to add it to a local branch

  2. git commit -m "message"

  3. to add to a remote repo, create a repo and then git remote add origin https://github.com/kurdi89/Django.git

  4. to push for the first time : git push -u origin master

for later changes use : git status, git add ., git push

to start an app :

  1. python manage.py startapp <app_name>

  2. register the app in the INSTALLED_APPS array in the settings.py

  3. create a basic view

  4. in the view.py inside the app folder add :

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

# Create your views here.
def test(request):
    return HttpResponse("<h1> Hello</h1>")

and in the urls.py file in the project folder add :

from django.contrib import admin
from django.urls import path
from app import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.test, name='test')
]

now visit localhost:8000/hello/ you should see an HTTP response of "Hello".

Creating a tamplate and passing contexts :

  1. create a template folder inside each app you create to handle all the templates, this folder should contain all the HTML files, in the app folder change the view.py to :
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def test(request):
    context = {
        "title" : "First Django app",
        "msg" : "Enjoy coding with Django",
    }
    return render(request, "index.html", context)

create index.html file in the template folder inside the app as :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{title}}</title>
</head>
<body>
    {{msg}}
</body>
</html>

and the urls.py in the main project should remain the same. visit, localhost:8000/hello


list view of a dictionary :

  1. update the views file and add :
def list_view(request):
    context = {
        "object_list": [
            {
                "title":"some title",
                "body":"some body description",
            },
            {
                "title":"some title",
                "body":"some body description",
            },
            {
                "title":"some title",
                "body":"some body description",
            },
        ],
        "title" : "this is the page title",
        "content" : "this should be a body for the page",
    }
    return render(request, 'list_page.html', context)

update the urlspatters in urls.py to :

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.test, name='test'),
    path('list-view/', views.list_view, name='list_view')
]

create an HTML page for the list view in the template folder name it list_view.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ title }}</title>
</head>
<body>
    {{  content  }} <br>

    the list : <br> 

    {%  for article in object_list  %}
        {{article.title}}: <br>
        {{article.body}} <br>
    {%  endfor  %}
</body>
</html>

visit : localhost:8000/list-view


Creating databses models :

  1. in your app, find models.py file and add your model like :
class Book(models.Model):
    title = models.CharField(max_length=120)
    summary = models.TextField()
    published_on = models.DateField() #auto_now_add=True 
    author = models.CharField(max_length=120)
    pages = models.PositiveIntegerField() #only positive numbers allowed
    def __str__(self):
        return self.title

always remember to make migrations after each time you create/update your database models, usually when you update it asks for 2 questions, set default manually

  1. run python manage.py makemigrations then python manage.py migrate

  2. register your model in your app folder, find admin.py and add:

from django.contrib import admin
from .models import Book

admin.site.register(Book)

Create an admin user :

  1. run python manage.py createsuperuser , enter username, email, password

your model should be registered for the admin panel, visit localhost:8000/admin/ to confirm.

migrating to a database, SQL or Postgres :

for Postgres , in settings.py, add the following in the DATABASES array and comment the sqlite3 option, ( important : you should already have postgres and pgadmin already installed on your machine)

to use Postgres database you will also need to install python package named : psycopg2

  • run only if you want to use Postgres : pip install psycopg2 then pip freeze > requirements.txt to add it to the list of requirments
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'Cool',
    'USER': 'postgres',
    'PASSWORD': 'postgres',
    'HOST': 'localhost',
    'PORT': '',
}


Create a super user for accessing the admin panel at localhost:8000/admin

in the project folder


Cloning a repo :

  • cd <directory>

  • install venv : virtualenv <venv> and <venv>\Scripts\activate

  • clone the repo : git clone <link>

  • pip install -r requirements