Django learning

How to use Django

  • install python

    1
    brew python
  • install pip

    1
    pip3 install pipenv
  • Create Django

    1
    pipenv install django==2.1

    This should create a virtual environment for you with Django install.

Pay Attention:
**Virtual environment specifies which wersion of Django you want to use for different project.**For example, if you have two projects, you’ll be able to create two separate virtual environments one for each. You can say something like, I want to use Django 2.1 for project A and Django 2.2 for Project B.
Use lsto check there are pipfile and pipfile.lock

  • Create a Django Project
  1. go to environment by texting pipenv shell
  2. create prject by typing django-admin startproject yourprojectname .
  3. start our django server by typing python manage.py runserver
  4. copy “http://127.0.0.1:8000/" and go to Chrome to open it

Hello world for Django

  • Create a new app within this project

PS: what is different between project and app in Django?
In Django:

  • Project:
    • a single project powers a single website
    • a project can contain multiple apps
  • App:
    • Each app focuses on a particular aspect
  • Example:
    • a shopping website:
    • one app for handling user authentication
    • another app for handling payments
    • another app for listing items

Open a new window in terminal and go the folder of your project;
Type:python manage.py startapp hello
Then use ls to see the file.

However, the Django project doesn’t know about hello we created.
To do that you can open ‘youproject/setting.py’

  • Create a view for the target URL
    here the target URL is 127.0.0:800/sayHello/
    A veiw = what’s shown to the user for a certain URL

Create a action page

  • Create a templates
    mkdir templates and create a html file

  • Create a models
    models = a way to interact with your database