Quick Django
Quick Django
1 . python -m pip install django
2 . python -m django --version
3 . django-admin startproject mysite
4 . cd mysite
5 . python manage.py runserver
5.5 python manage.py runserver localhost:8888
ตอนแรกที่สร้าง จะมี Folder ชื่อเดียวกับ ตอนที่ startproject
เราต้อง Create Application ขึ้นมาเพื่อใช้งานก่อน
มันจะเป็นเหมือน Project Solution
-------------------------------------------------------------------------------
pip Package ที่ต้องใช้ในโปรเจค
- pip install psycopg2
- pip install requests
-------------------------------------------------------------------------------
Note (โครงสร้างโปรเจค Django Framework) :
manage.py คือ script สำหรับรันคำสั่งต่างๆ ที่เกี่ยวข้องกับ Django เช่น Run Server , Collectstatic , Model & Migration เป็นต้น
__init__.py คือ initial ไฟล์หรือไฟล์เปล่าๆมีไว้เก็บ Python Package เราสามารถเพิ่ม Script การทำงานเข้าไปในไฟล์นี้ได้
settings.py คือ ไฟล์ที่ใช้สำหรับการตั้งค่าโปรเจคเช่น การตั้งค่าแอพ , เวลา , Path,ฐานข้อมูลที่ใช้ เป็นต้น
urls.py คือ ไฟล์ที่ใช้เก็บการ routing ของ HTTP request หรือเรียกอีกอย่างว่าการกำหนด url pattern ของ django project
wsgi.py คือ ไฟล์ที่ใช้เก็บข้อมูลโปรเจคสำหรับการ Deployment (Production)
-------------------------------------------------------------------------------
__Projects vs. apps
Command
- py manage.py startapp App
Projects
- A project is a collection of configuration and apps for a particular website.
- A project can contain multiple apps.
- An app can be in multiple projects.
Apps
- An app is a Web application that does something – e.g., a Weblog system,
a database of public records or a small poll app.
-------------------------------------------------------------------------------
Create View + Controller + Route
- Django เป็นสถาปัตยกรรมแบบ MVT (Model View Template)
1__Generate App
- py manage.py startapp polls
-------------------------------------------------------------------------------
2__Create App Controller
<pre file="polls/view.py">
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
</pre>
-------------------------------------------------------------------------------
3__Create Route
<pre file="urls.py">
from Home import views as HomeView
(from {{AppName}} import {{FileView}} as {{VarAppName}})
urlpatterns = [
path('', HomeView.index)
]
</pre>
-------------------------------------------------------------------------------
3.1 __ By Pass CSRF
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def callService1(request):
pass
-------------------------------------------------------------------------------
4__Create Folder Templates
สร้าง Folder Templates เอาไว้ชั้นเดียวกับ manage.py
-------------------------------------------------------------------------------
5__Set Templates Path
เข้าไปที่ไฟล์ settings.py
<pre file="settings.py">
import os
TEMPLATES = [
{
'DIRS': [
os.path.join(BASE_DIR, "templates")
],
}
]
</pre>
-------------------------------------------------------------------------------
6__Create Index.html
- "Home.Index.html"
-------------------------------------------------------------------------------
7__Apply Template to App
<pre>
from django.shortcuts import render
def Index(request):
return render(request, "Home.Index.html")
</pre>
-------------------------------------------------------------------------------
8__Add CSS & JS to Static Folder
สร้าง static folder ไว้ชั้น manage.py
- static > css, js
<pre file="settings.py">
STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
</pre>
<pre type="Home.Index.html">
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" />
</pre>
-------------------------------------------------------------------------------
9_Create Layout
สร้างไฟล์ Layout.Home.html ที่ Templates
<pre file="Home.Index.html">
{% extends 'Layout.Home.html' %}
{% block content %}
<h1> Content is here</h1>
{% endblock %}
</pre>
-------------------------------------------------------------------------------
10_Include Html File
เอาไฟล์ html เข้ามาแทรก
{% include 'Nav.Top.html' %}
-------------------------------------------------------------------------------
11_Set Database Postgresql
- pip install psycopg2
<pre file="settings.py">
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'Gsb_CreaditBureau_Web',
'USER': 'postgres',
'PASSWORD': 'Password@1234',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
</pre>
-------------------------------------------------------------------------------
12_ Migrate initial (Init + Create )
- พิมพ์คำสั่ง py manage.py migrate // ใช้สร้าง init สำหรับ authen django
- พิมพ์คำสั่ง py manage.py makemigrations //
Note : migration มันจะสร้างตามแอพแต่ละตัว
ต้องเข้าไปเพิ่ม App ใน settings ก่อน เพื่อให้ django รู้จักแอพที่เราสร้างขึ้น
<pre file="settings.py">
INSTALLED_APPS = [
...
'Home'
]
</pre>
13_ Create Admin USER
- พิมพ์คำสั่ง py manage.py createsuperuser --username=admin --email=admin@mail.com
14_ Create User Use Code
- from django.contrib.auth.models import User
- user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
- user.last_name = 'Lennon'
- user.save()
15_ การใช้งาน AngularJs ร่วมกับ syntax django
https://stackoverflow.com/questions/38257903/cant-display-data-from-http-get-with-angular-django
<pre>
app.config(['$httpProvider', '$interpolateProvider', function($httpProvider, $interpolateProvider) {
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');
}]);
</pre>
ความคิดเห็น
แสดงความคิดเห็น