Iterator

L =…

concurrent.futures

ThreadPoolExecutor

import requests
from concurrent import futures
to_do = []def get_resource(url):
return requests.get(url)
""" First way """
with futures.ThreadPoolExecutor(max_workers = 3) as executor:
for url in…

Program

Process

CORS (Cross-Origin Resource Sharing)

When we develop Django REST API would need to interact with other applications that hosted on different domains. For these requests, we should enable CORS in our server.

Install required package

pip3 install django-cors-headers

Update installed apps in settings.py

INSTALLED_APPS = [
... ,
'corsheaders',
]

Update middleware in settings.py

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
... ,
]

Enable CORS in settings.py

CORS_ORIGIN_ALLOW_ALL = True

python manage.py runserver ––noreload

python manage.py check — database default

django.core.exceptions.ImproperlyConfigured: Requested setting XXX, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

DJANGO_SETTINGS_MODULEWhen you use Django, you have to tell it which settings you’re using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'RealEstateProject.settings')

Setup MySQLdb

import pymysql
pymysql.install_as_MySQLdb()
PyMySQL source code (__init__.py)

Setup MySQL engine

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'real_estate_db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'real_estate_data',
'USER': 'root',
'PASSWORD': '1209',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
from DRFapp.models import User
from DRFapp.serializers import UserSerial
class UserUpdateViewset(viewsets.ModelViewSet): queryset = User.objects.all()…

Install required package

pip3 install coreapi

Set the schema in settings.py

REST_FRAMEWORK = {
"DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema"
}

Set the correspond url in urls.py

from rest_framework.documentation import include_docs_urls

urlpatterns = [
path(r'admin/', admin.site.urls),
path(r"api/doc/", include_docs_urls(title="API Document"))
]

Rex Chiang

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store