Cheat Sheets

Aug. 18, 2020

Tabs

django-admin startproject mysite
python manage.py runserver 0.0.0.0:8000
python manage.py startapp polls
python manage.py makemigrations [app]
python manage.py migrate [app]

python manage.py shell or dbshell
python manage.py createsuperuser
python manage.py changepassword 

python manage.py  app


python -m pip install Django python -m django --version >>> import django >>> print(django.get_version())
from polls.models import Choice, Question
Question.objects.all()
from django.utils import timezone
q = Question(question_text="What's new?", pub_date=timezone.now())
q.save()
[Question.create(question_text="What's new?", pub_date=timezone.now())]
q.id
q.question_text
q.pub_date
q.question_text = "What's up?"
q.save()
Question.objects.all()
Question.objects.filter(id=1)
Question.objects.filter(question_text__startswith='What')
current_year = timezone.now().year
Question.objects.get(pub_date__year=current_year)
Question.objects.get(id=2)
Question.objects.get(pk=1)
q = Question.objects.get(pk=1)
q.was_published_recently()
q.choice_set.all()
q.choice_set.create(choice_text='Not much', votes=0)
q.choice_set.create(choice_text='The sky', votes=0)
c = q.choice_set.create(choice_text='Just hacking again', votes=0)
c.question
q.choice_set.all()
Choice.objects.filter(question__pub_date__year=current_year)
Choice.objects.filter(question__pub_date__day=8)
c = q.choice_set.filter(choice_text__startswith='Just hacking')
c.delete()
--------------------------------------------------------

>>> reverse('polls:results', args=(1,))
'/polls/1/results/'

--------------------------------------------------------

from django.test.utils import setup_test_environment
setup_test_environment()
from django.test import Client
client = Client()
response = client.get('/')
response = client.get('/polls/1/')
response.status_code
from django.urls import reverse
response = client.get(reverse('polls:index'))
response.status_code
response.context['latest_question_list']
response.content

c = Client()
response = c.post('/login/', {'username': 'john', 'password': 'smith'})
response.status_code
response = c.get('/customer/details/')
response.content

git init
git config --global user.name "username"
git config --global user.email mymail@yahoo.com
vim .gitignore   (files to be ignored)
git status
git add --all .
git commit -m "Django app, test commit"
#new repository
git remote add origin https://github.com/username/test2.git
git push -u origin master
#cloning
git clone https://github.com/username/test.git

pipenv install
pipenv shell
pipenv install django

virtualenv env
source env/bin/activate
pip install -r requirements.txt
pip install ...
(pip freeze > requirements.txt)
django-admin.py startproject myproject .

python3.7 -m venv env37
virtualenv -p python3.6 env36
virtualenv --python=/usr/bin/python3.6 env36

source env37/bin/activate
source env36/bin/activate

Return to home