Friday, April 24, 2020

Django pollapp - 1( Installation and set up)


Let’s start learning development with Django by creating a basic poll application.



It’ll consist of two parts:



A public site that lets people view polls and vote in them.

An admin site that lets you add, change, and delete polls.



Create a new directory for your project called Application_django, switch to that directory in a terminal, Cd C:\Application_django and enter the following code to create a virtual environment:



python -m venv pollapp_env



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



Next we need to activate the virtual environment using the following command:



pollapp_env \Scripts\activate



When the environment is active, you’ll see the name of the environment in parentheses. then you can install packages to the environment and use packages that have already been installed. Packages you install in pollapp_env will be available only while the environment is active. The command prompt should look like this:



Microsoft Windows [Version 10.0.18362.778]

(c) 2019 Microsoft Corporation. All rights reserved.



C:\Users\vswam>cd C:\Application_django



C:\Application_django>python -m venv pollapp_env



C:\Application_django>pollapp_env\Scripts\activate



(pollapp_env) C:\Application_django>



Once the virtual environment is activated, enter the following to install Django:



(pollapp_env) C:\Application_django> pip install Django



Successfully installed asgiref-3.2.7 django-3.0.5 pytz-2019.3 sqlparse-0.3.1



Keep in mind that Django will be available only when the pollapp_env environment is active.



Without leaving the active virtual environment enter the following commands to create a new project:



(pollapp_env) C:\Application_django>  django-admin startproject pollsite .

The dot at the end of the command 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.



Now look at the file structure of the created project:



Application_django /
    manage.py
    pollsite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

These files are:

  • The outer Application_django/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner pollsite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. pollsite.urls).
  • pollsite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • pollsite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • pollsite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • pollsite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • pollsite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.



Let’s make sure that Django has set up the project properly. Enter the runserver command as follows to view the project in its current state:



(pollapp_env) C:\Application_django>python manage.py runserver

Watching for file changes with StatReloader

Performing system checks...



System check identified no issues (0 silenced).



You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.

Run 'python manage.py migrate' to apply them.

April 24, 2020 - 12:25:09

Django version 3.0.5, using settings 'pollsite.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.



Django should start a server called the development 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 it to the browser.



The URL http://127.0.0.1:8000/ indicates that the project is listening for requests on port 8000 on your computer, which is called a localhost. The term localhost refers to a server that only processes requests on your system; it doesn’t allow anyone else to see the pages you’re developing.



Open a web browser and enter the URL http://localhost:8000/, or http:// 127.0.0.1:8000/ if the first one doesn’t work. You should see something like :







A page that Django creates to let you know all is working properly so far. Keep the server running for now, but when you want to stop the server, press ctrl-C in the terminal where the runserver command was issued.

This is a page that Django creates to let you know all is working properly so far.  If you are unable to see this page then you probably made some mistake.

Here I am ending today's post. In the next post we'll move on to create our first app for the website project. Till we meet next, keep practicing and learning Python as Python is easy to  learn.
Share:

0 comments:

Post a Comment