Creating a 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 the 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 should 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 the 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 the web server, and connect with the database.

Open the 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. def index(response):
    3. 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. urlpatterns = [
    4. path ('', views.index)
    5. ]
    Define list objects called urlpattern – in this list we map various urls to their view functions.
    To reference these URLs, the 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. urlpatterns = [
    4. path('admin/', admin.site.urls),
    5. ]
    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 the 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 the imported path as shown below
    1. from django.contrib import admin
    2. from django.urls import path, include
    3. urlpatterns = [
    4. path('admin/', admin.site.urls),
    5. path('Products/', include('Products.urls'))
    6. ]
    Creating Python WebApp Using Pycharm And Django Environment Setup And Navigation URL
  1. Go to the 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…. :)