Skip to content

Instantly share code, notes, and snippets.

@meet59patel
Last active February 24, 2024 23:33
Show Gist options
  • Save meet59patel/4efaa2f52909970b578c40d3fbf061f5 to your computer and use it in GitHub Desktop.
Save meet59patel/4efaa2f52909970b578c40d3fbf061f5 to your computer and use it in GitHub Desktop.
Getting started with Django (2.2.4) - with virtual env setup
Make your project directory.
Open terminal with the project directory as current directory.
Make sure python3 and pip installed on system.
Run this command: $ python3 -m venv myvenv
virtual environment called 'myenv' has been created for your current project.
To activate the virtual environment: $ source myvenv/bin/activate
Make requirements.txt file in main project directory. Add this line: Django~=2.2.4
(You can add more requirements later.)
Save requirements.txt file and run this command: $ pip install -r requirements.txt
Above will install current requirement python packages to the virtual environment.
to check installed packages list: $ pip freeze
(note: https://youtu.be/e1IyzVyrLSU )
(Make sure you have virtual environment activated.)
$ django-admin startproject myproject
Default database is sqlite3
To 'Migrate' the default boilerplate databases(admins, default apps etc.): $ python manage.py migrate
To run server(by default port: 8000): $ python manage.py runserver
To run server on different port: $ python manage.py runserver 8080
ctrl + c to stop running server.
To create new App (for example 'polls'): $ python manage.py startapp polls
In settings.py add your app to the INSTALLED_APPS list
Go to the app directory you want and edit the models.py
Here make classes like Question, Choice which are extended from models.Model class
Trick: To display some attribute of object instead of 'object 1' like names in Admin panel, add __str__() method in class.
migrations:
1) This step makes database model of python class you created (which is stored in 'migrations' folder)
$ python manage.py makemigrations
2) This step migrates the database model and adds table to the database.
$ python manage.py migrate
After this follow the "Using the shell" portion...

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
Create admin from above steps.
Currently Question and Choice objects won't be visible in Admin panel. For those to be visible, add this to polls->admin.py file:
from .models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
Now you can see and edit Question/Choice directly from Admin panel.
To manipulate more items and how they appear in admin panel, edit admin.py

Source - MDN Web Docs | virtualenvwrapper

sudo pip3 install virtualenvwrapper
$ mkvirtualenv my_django_environment

image

@rutuukulkarni
Copy link

commands are not working properly.

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