Là một khung công tác Web Python, Django yêu cầu Python.Python bao gồm một cơ sở dữ liệu nhẹ được gọi là SQLite, vì vậy bạn sẽ không cần phải thiết lập một cơ sở dữ liệu.
Tải xuống phiên bản Python mới nhất tại https://www.python.org/downloads/
Tải Pycharm về.
Mở Pycharm lên tạo mới project, Mở terminal của project vừa tạo lên tiến hành cài đặt Django bằng lệnh sau :
pip install Django
Chạy project lên bằng lệnh sau : django-admin startproject tên_Project
Vào project bằng lệnh: cd python_Project
Chạy câu lệnh : python manage.py runserver Để chạy sever của mình lên.
Chạy lênh : python manage.py startapp polls Để tạo một app mới tên là polls.
Trong app polls có các thành phần :
- migrations lưu lại các lịch sử bạn tạo và thay đổi database như thế nào.
- __init__.py là một file trống xem như là module của python.
- apps.py định nghĩa tên của application.
- admin.py dùng để đăng ký các module trong modules.py
- modules.py dùng để tạo các module. - tests.py dùng để cho các bạn viết testcase củ các bạn.
- views.py nơi hiển thị các giao diện.\
1/ Tạo dự án mới mySite
Vào Polls views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse(" hello")
# Create your views here.
Vào Polls Tạo urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Vào mySite vào settings.py:
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Vào mySite vào urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Chạy server: python manage.py runserver

0 Nhận xét