Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Commands for Django 2.x Crash Course

Django Crash Course Commands

# Install pipenv
pip install pipenv
# Create Venv
pipenv shell
# Install Django
pipenv install django
# Create project
django-admin startproject pollster
cd pollster
# Run server on http: 127.0.0.1:8000 (ctrl+c to stop)
python manage.py runserver
# Run initial migrations
python manage.py migrate
# Create polls app
python manage.py startapp polls
# Create polls migrations
python manage.py makemigrations polls
# Run migrations
python manage.py migrate
# Using the shell
python manage.py shell

>>>  from polls.models import Question, Choice
>>>  from django.utils import timezone
>>>  Question.objects.all()
>>>  q = Question(question_text="What is your favorite Python Framework?", pub_date=timezone.now())
>>>  q.save()
>>>  q.id
>>>  q.question_text
>>>  Question.objects.all()
>>>  Question.objects.filter(id=1)
>>>  Question.objects.get(pk=1)
>>>  q = Question.objects.get(pk=1)
>>>  q.choice_set.all()
>>>  q.choice_set.create(choice_text='Django', votes=0)
>>>  q.choice_set.create(choice_text='Flask', votes=0)
>>>  q.choice_set.create(choice_text='Flask', votes=0)
>>>  q.choice_set.all()
>>>  quit()
# Create admin user
python manage.py createsuperuser
# Create pages app
python manage.py startapp pages
@t-eckert

This comment has been minimized.

Copy link

@t-eckert t-eckert commented Feb 17, 2020

Quick error I noticed:

When adding the question and choices to the database, you add "Flask" as an option twice. In the tutorial, you add "Web2py" the second time.

@h3ndry

This comment has been minimized.

Copy link

@h3ndry h3ndry commented Mar 18, 2020

Thank you so much

@deeprogra

This comment has been minimized.

Copy link

@deeprogra deeprogra commented Mar 24, 2020

Thank you so much

The same

@Nannim

This comment has been minimized.

Copy link

@Nannim Nannim commented Apr 15, 2020

gyop

@kleva-j

This comment has been minimized.

Copy link

@kleva-j kleva-j commented Apr 15, 2020

@Nannim Do you mean Grow Your Own Potatoes.

@Dsabillon

This comment has been minimized.

Copy link

@Dsabillon Dsabillon commented May 12, 2020

Thanks!!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 31, 2020

Thanks a lot!

@Mseleem

This comment has been minimized.

Copy link

@Mseleem Mseleem commented Mar 4, 2021

Hi
at
python manage.py makemigrations polls

Traceback (most recent call last):
File "manage.py", line 22, in
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/core/management/init.py", line 377, in execute
django.setup()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 779, in exec_module
File "", line 916, in get_code
File "", line 846, in source_to_code
File "", line 219, in _call_with_frames_removed
File "/home/mseleem/Desktop/djangoProject/djangoProject/polls/models.py", line 5
question_text = models.CharField(max-length=200)
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

What could I do?
Thank you

@kendrickchibueze

This comment has been minimized.

Copy link

@kendrickchibueze kendrickchibueze commented Mar 16, 2021

Brad you are so excellent.Thanks for your clearity

@Keileb09

This comment has been minimized.

Copy link

@Keileb09 Keileb09 commented Mar 16, 2021

Hi
at
python manage.py makemigrations polls

Traceback (most recent call last):
File "manage.py", line 22, in
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/core/management/init.py", line 377, in execute
django.setup()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/mseleem/.local/share/virtualenvs/djangoProject-yKaAz5Ml/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 779, in exec_module
File "", line 916, in get_code
File "", line 846, in source_to_code
File "", line 219, in _call_with_frames_removed
File "/home/mseleem/Desktop/djangoProject/djangoProject/polls/models.py", line 5
question_text = models.CharField(max-length=200)
^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

What could I do?
Thank you

change max-length for max_length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment