django

August 6, 2020 Ricardo Santos Cheat sheet 0 minutes, 36 seconds

Requirement.txt

django

Start project

django-admin.py startproject 2onlinesync .
mv 2onlinesync 2onlinesync-project
cd 2onlinesync-project

inicializar o pipenv

pipenv install

instalar django no pipenv

pipenv install django

entrar na shell do env criado pelo pipenv

pipenv shell

create app

python manage.py startapp api

Json response

from django.http import JsonResponse
from .models import Product, Manufacturer

def product_list(request):
    products = Product.objects.all() # [:30]
    data = {"products": list(products.values())} # .values("pk", "name")
    return JsonResponse(data)

html response

from django.http import HttpResponse

def myview(request):
    return HttpResponse("return this string")
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Initial Data

Login to the admin panel and create some test data

Dump the data:

python manage.py dumpdata > data/initial.json

Run the data to test that it works:

python manage.py loaddata data/initial.json