Last active
June 3, 2018 14:36
-
-
Save donrokzon/88711510245f584a9ad07bb7f5b9ccdf to your computer and use it in GitHub Desktop.
Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir tutorial | |
cd tutorial | |
# Create a virtualenv to isolate our package dependencies locally | |
virtualenv env | |
source env/bin/activate # On Windows use `env\Scripts\activate` | |
# Install Django and Django REST framework into the virtualenv | |
pip install django | |
pip install djangorestframework | |
# Set up a new project with a single application | |
django-admin.py startproject tutorial . # Note the trailing '.' character | |
cd tutorial | |
django-admin.py startapp quickstart | |
cd .. | |
python manage.py migrate | |
python manage.py createsuperuser --email admin@example.com --username admin | |
pip install django-mysql | |
pip install mysqlclient | |
'default': { | |
'ENGINE': 'django.db.backends.mysql', | |
'NAME': 'mydatabase', | |
'USER': 'root', | |
'PASSWORD': 'root', | |
'HOST': '', | |
'PORT': '', | |
'OPTIONS': { | |
# Tell MySQLdb to connect with 'utf8mb4' character set | |
'charset': 'utf8mb4', | |
}, | |
# Tell Django to build the test database with the 'utf8mb4' character set | |
'TEST': { | |
'CHARSET': 'utf8mb4', | |
'COLLATION': 'utf8mb4_unicode_ci', | |
} | |
} | |
./manage.py check | |
mysql -u root -p | |
urls.... | |
from django.urls import path,include | |
path('test/', include('MyApp.urls')), | |
models.......... | |
class Employees(models.Model): | |
emp_first_name = models.CharField(max_length=20) | |
emp_last_ame = models.CharField(max_length=20) | |
emp_age = models.IntegerField() | |
def __str__(self): | |
return self.first_name | |
views....... | |
from rest_framework.decorators import api_view | |
from rest_framework.response import Response | |
@api_view(['GET', 'POST']) | |
def index(request): | |
if request.method == 'POST': | |
return Response({"message": "Got some data!", "data": request.data}) | |
return Response({"message": "Hello, world!"}) | |
........... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment