Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL

In this article, you will learn how to create Python webapp using Pycharm and Django environment setup and navigation URL.

Creating Python web app using Pycharm and Django Framework

  • Part 1 - Environment Setup
  • Part 2 - Mapping Navigation URL
Steps to follow,
  1. Go to python.org and download and install the latest version of Python from the download section

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. After installation, the next step is to go to download and install the latest Pycharm Community from here.
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Open Pycharm

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Click on create New Project – Enter name of project and click Create

    New Python Project will be created in Pycharm

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Open Terminal by clicking on Terminal option present at the bottom left side of pycharm window
  1. Install the latest Django framework by typing the following command on terminal opened in above step

    >>pip install Django==2.2.6

    (here 2.2.6 is the latest framework as of now, you can check for the latest version by browsing Django official website.)

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Create a new Django app inside the project by using the following command (don’t forget to write dot at the end of the command, containing space between project name and dot)

    >>django-admin startproject Helloworld .

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. The above command will create a New folder named Helloworld with four files and one manage.py as shown below,

    Helloworld
    | __init__.py
    | setting.py
    | urls.py
    | wsgi.py
    | manage.py

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Run the server by using manage.py file created earlier by using the following command,

    >>python manage.py runserver

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL

  2. Open the browser and type http://127.0.0.1:8000/ in the address bar

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Congratulations…! Environment Set up is done successfully

Part 2 - Creating Python web app using Pycharm and Django Framework – Mapping Navigation URL

 
Define what the  user shoul see when the user browses through e.g.,
 
http://127.0.0.1:8000/Products/
http://127.0.0.1:8000/About/
http://127.0.0.1:8000/Contacts/
 
Steps to follow,
 
Previously we have created a Helloworld app under the project “My project" with four files.
 
Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
 
Directory Helloworld contains four subfiles
  1. _init_.py - Helloworld directory is nothing but package defined under the project and we can import various modules from this package into other modules
  2. settings.py – We can define various setting for the application
  3. urls.py – With this module we can define what the user should see when the user browses through e.g. - /Product, /about, /contact
  4. wsgi.py – wsgi stands for webserver gateway interface, the purpose of this module to provide a standard interface between application with Django and web server.
  5. manage.py – this module is used to manage the Django project. With this we can start web server, and connect with database.

Open terminal window again, which is at the bottom left side of pycharm editor

 
Run the server by using the command,
 
>>python manage.py runserver
 
Click on plus sign (+) in terminal to open a new terminal.
 
The project will divide into multiple apps, so we are going to create a new app by triggering the following command,
 
>> python manage.py startapp Products
 
New app will be added under Myproject, we will use views.py module to setup navigations.
 
Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Open views.py

    Import HttpResponse from Django to return simple text message from Products App
    1. from django.http import HttpResponse  
    2.   
    3. def index(response):  
    4.     return HttpResponse("This message is from Products App")  
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL

    Whenever user asks for /Product we need to call index function.

    Let’s do mapping
  1. Create a new urls.py file under Products App by right clicking on Products folder
  2. Open the urls.py file
    1. from django.urls import path  
    2. from . import views  
    3.   
    4. urlpatterns = [  
    5.     path ('', views.index)  
    6. ]  
    Define list objects called urlpattern – in this list we map various urls to their view functions.
    To reference these URLs, import path function from django.url.
    1. from django.urls import path  
    To provide a second parameter/passing reference of views.index function (i.e. views.index) to list item path we need to import views
    1. from . import views  
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Now we will need to tell django about a newly-created app (i.e Products)

  2. Collapse the Products folder and open the Helloworld folder and open urls.py module under root (Helloworld) folder

    You will find following window
    1. from django.contrib import admin  
    2. from django.urls import path  
    3.   
    4. urlpatterns = [  
    5.     path('admin/', admin.site.urls),  
    6. ]  
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL

    Every django project comes with an administrative column.

    Any request with the keyword admin/ will go to administrative panel, in the same way, any request which comes with Products/ keyword will go to Products apps.

    Add another object in urlpattern of root urls.py file

    To do that we need to add “include” function in imported path as shown below
    1. from django.contrib import admin  
    2. from django.urls import path, include  
    3.   
    4. urlpatterns = [  
    5.     path('admin/', admin.site.urls),  
    6.   
    7.     path('Products/', include('Products.urls'))  
    8. ]  
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Go to browser and put the URL http://127.0.0.1:8000/Products/ (Make sure server is running)

    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
The message from views.py under Products app will show on the screen…
 
Happy Coding…. :)