Django poll application is a polling app.
In this poll app project where several polls(Question) are there and you are supposed to vote for them and see the poll results. I have created a model name Poll(Question) and a poll has a question and three options(Choice).Each options is associated with a question of poll.
- Create Poll
- Show all poll list
- Vote for each poll
- View poll results
- Apply pagination (i.e Each page contains only three poll(Questions))
- Python, Django,Html, CSS, Bootstrap
- Python IDE
- VS Code (Visual Studio Code)
- Install following extensions : -
- Python
- Code runer
- Download Python and Install
- python.exe path must be set in 'path' environment variable
- For more details link are given below:-
-
To check python latest version:
python --version
-
To check pip:
pip --version
-
To check all packages in your system:
pip freeze
-
To check Django install in your sytem or not,type:
django-admin --version
-
To install requirements.txt file, type :
pip install -r requirements.txt
-
Used to two extra packages for this poll application
pip install django-widget-tweaks
run this command and also install insettings.py
file.This command is used for styling the widgets so that website look good.pip install pylint
This command is used to handle the problem such as unable to import Django.shortcuts For more details click here.- How to protect your Django secret key using the .env file official website
- How to deploy a Django app website to Heroku using Github Repository
-
First we need to create a virtual environment.Using a virtual environment avoids installing Django into a global python environment and we will have exact control over the libraries used in an application.
- Step-1:- Create a project folder on file system like 'Project-django' and open inside VS code.
- Step-2:- Install django in Separate environment.
-
We can create virtual environment two ways.
-
First way (Open CMD or Terminal)
- Install virtual environment wrapper :
pip install virtualenvwrapper-win
- Create a new virtual environment :
mkvirtualenv Env_Name
Example :mkvirtualenv poll_env
- To activate virtual environment :
workon Env_Name
Example :workon poll_env
- Install Django :
pip install django
- How to setup virtual environment for Django project
- Using this, virtual environment is install in your default working directory(C Drive).In Envs folder virtual environment(Env_Name) is present.
- Note : - In terminal virtual environment not seen.In CMD virtual environment see after activate it.To deactivate virtual environment simply type
deactivate
and to delete virtual environement ,typermvirtualenv Env_Name
and Uninstall django,typepip uninstall django
- Install virtual environment wrapper :
-
Another way to create virtual environment (Open terminal or CMD)
- Create a new virtual environment:
python -m venv Env_Name
- To activate virtual environment:
Env_Name\Scripts\activate.bat
But on Unix or MacOS:source Env_Name/bin/activate
- Install Django :
pip install django
- How to setup virtual environment for Django project
- Using this way, virtual environment is install in your current directory.
- Note : - In terminal virtual environment not seen.In CMD virtual environment see after activate it.To deactivate virtual environment simply type
deactivate
and to delete virtual environement ,typermdir Env_Name /s
and Uninstall django,typepip uninstall django
- Create a new virtual environment:
- Activate virtual environment :
workon Env_Name
- Create Django Project :
django-admin startproject PROJECT_NAME
- Create Django Application :
python manage.py startapp APPLICATION_NAME
- Install your application in
settings.py
file. - Run the sever :
python manage.py runserver
- Open website in browser at
http://localhost:8000
or admin athttp://localhost:8000/admin
- Quit the server :
ctrl+c
- Whenever you edit your model fields (adding a new one, changing an existing one or altering any of the arguments it takes) then you should always run migrations.
python manage.py makemigrations <app>
: Create the migrations (generate the SQL commands). python manage.py migrate
: Run the migrations (execute the SQL commands).- To create super user, type :
python manage.py createsuperuser