Thursday, October 24, 2019

PollOpinion site in Django-1 ( Preparing the project)

We'll create a Poll Opinion site in Django which will post questions and provide answers as options to choose from. But first we need to describe the project in a specification,or spec. Then we’ll set up a virtual environment to build the project in.

1. Let's create our project folder PollSite in our directory:

C:\Projects\PollSite

2. Now switch to this directory in a terminal, and create a virtual environment:

a. cd C:\Projects\PollSite

b. python -m venv pollsite_env (command to create a virtual environment)

Here we’re running the venv module and using it to create a virtual environment named pollsite_env. 

After the virtual environment is created your command window should look like:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


C:\Users\Python>cd C:\Projects\PollSite

C:\Projects\PollSite>python -m venv pollsite_env


C:\Projects\PollSite>

3. After we have a virtual environment set up, we need to activate it with the following command:

pollsite_env\Scripts\activate

When the environment is active, you’ll see the name of the environment in parentheses, as shown:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Python>cd C:\Projects\PollSite

C:\Projects\PollSite>python -m venv pollsite_env

C:\Projects\PollSite>pollsite_env\Scripts\activate

(pollsite_env) C:\Projects\PollSite>


4. Now it's time to install Django with the help of following command:

(pollsite_env) C:\Projects\PollSite>pip install django

As you can see we are installing the django in our virtual environment. After the installation is done you command window will look something like this:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Python>cd C:\Projects\PollSite

C:\Projects\PollSite>python -m venv pollsite_env

C:\Projects\PollSite>pollsite_env\Scripts\activate

(pollsite_env) C:\Projects\PollSite>pip install django
Collecting django
  Using cached https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b
7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl
Collecting pytz (from django)
  Using cached https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5
d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-2.1.7 pytz-2018.9
You are using pip version 9.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' comm
and.

(pollsite_env) C:\Projects\PollSite>


5. After the django installation is done we now create our Poll_Opinion project as shown:

django-admin.py startproject Poll_Opinion .

The dot in the end of the above command is not a full stop and it creates the new project with a directory structure that will make it easy to deploy the app to a server when we’re finished developing it.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.


(pollsite_env) C:\Projects\PollSite> django-admin.py startproject Poll_Opinion .
 
 (pollsite_env) C:\Projects\PollSite>

Now check  Poll_Opinion directory. It contains four files which are settings.py, urls.py, and wsgi.py.



The settings.py file controls how Django interacts with your system and manages your project. We’ll modify a few of these settings and add some settings of our own as the project evolves.

The urls.py file tells Django which pages to build in response to browser requests. The wsgi.py file helps Django serve the files it creates. The filename is an acronym for web server gateway interface.

6. Next we need to create a database that Django can work with as Django stores most of the information related to a project in a database. To create the SQLite database for the PollOpinion  project, enter the following command (still in an active environment):

 (pollsite_env) C:\Projects\PollSite> python manage.py migrate 

The command window shows the database creation as shown:


(pollsite_env) C:\Projects\PollSite>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

(pollsite_env) C:\Projects\PollSite>
Whenever we modify a database, we say we’re migrating the database. Issuing the migrate command for the first time tells Django to make sure the database matches the current state of the project. The first time we run this command in a new project using SQLite, Django will create a new database for us.


7. Let’s make sure that Django has set up the project properly by running the server. Enter the following command in your command window:

 (pollsite_env) C:\Projects\PollSite>python manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).
March 06, 2019 - 14:54:32
Django version 2.1.7, using settings 'Poll_Opinion.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.





Django starts a server so you can view the project on your system to see how well it works. When you request a page by entering a URL in a browser, the Django server responds to that request by building the appropriate page and sending that page to the browser.

As shown in the command window, Django checks to make sure the project is set up properly; then
it reports the version of Django in use and the name of the settings file being used;  reports the URL where the project is being served. The URL http://127.0.0.1:8000/ indicates that the project is listening for requests on port 8000 on our computer.

Now open a web browser and enter http://127.0.0.1:8000/ . We will see a page that Django created to let us know all is working properly:












 





Share:

0 comments:

Post a Comment