django-polling_site
django-polling_site ( Life's First Django Project)
HOW TO RUN ON YOUR COMPUTER
pre-requisites : python(3.8 or above) and pip must be installed
1. CREATE AND ACTIVATE A VIRTUAL ENVIRONMENT INSIDE A NEW DIRECTORY
Virtual Environments are a great way to wrap python projects. I have stopped working on this repo on Jul 15 2020. At the time of my work I have used the latest versions of python and django. If you are reading this after 2020, chances are new versions of python and django might have rolled out . So to ensure that old code runs smoothly , create a virtual environment.
Using Virtual Environments ensures the dependancies of each project is independently availaible and there is no headache of breakage of code and EVERY PROJECT STAYS INDEPENDENT OF ONE ANOTHER
IF YOU ARE NEW TO VIRTUAL ENVIRONMENTS READ PYTHON-3 OFFICIAL DOCS
2. CLONE THIS REPO django-polling-site
IN YOUR DIRECTORY, AND INSTALL THE REQUIREMENTS
pip install -r requirements.txt
3. MOVE INSIDE THE pollsite
DIRECTORY AND MAKE MIGRATIONS
python manage.py makemigrations poll
python manage.py makemigrations home
python manage.py migrate
4. CREATE A SUPERUSER , WITH YOUR OWN USERNAME AND PASSWORD
python manage.py createsuperuser
5. POPULATE DATABASE WITH DUMMY DATA OF QUESTIONS AND CHOICES, BY RUNNING THE populator_script
FROM DJANGO SHELL
click here to see how
6. Inside the home
app directory , configure the homePage.yaml according to your wish
Inside views.py
of same directory, put the absolute path of homePage.yaml in the place instructed.
NOW RUN THE SERVER
python manage.py runserver
Go to http://127.0.0.1:8000/ . This is where Django starts server by default
HOLA !! ENJOY YOU HAVE SUCCESSFULLY RUN THE SERVER
Click on Admin button on top right corner to go the the Django Administration page. You can add team members , change or add questions and choices
Vist DJANGO'S OFFICIAL WEBSITE FOR MORE DETAILS..
you can now play around with the code your self
RUNNING SCRIPTs FROM DJANGO SHELL
inside apps you can't write your own scripts because they are run by django. Running a module directly will give lots of import errors
YOU WILL GET ERRORS IF YOU RUN THESE SCRIPT IN CWD CONTEXT
DON'T RUN DIRECTLY LIKE
python3 script.py
The exec()
Function
exec()
function is used for the dynamic execution of Python program
which can either be in form of a string or object code. If it is a string,
the string is parsed as a suite of Python statements which is then executed
unless a syntax error occurs and if it is an object code, it is simply executed.
- START THE DJANGO SHELL BY RUNNING
$ python3 manage.py shell
(because Django Shell offers special access features)
- LOAD THE SCRIPT IN A VARIABLE
>>> script = open(script_path).read()
- EXECUTE THE SCRIPT IN PYTHON CONSOLE RUNNING IN DJANGO CONTEXT
>>> exec(script)
UPDATE : A BETTER WAY
python3 manage.py shell < myScript.py