This space is available for advertisement. Please contact us if interested.
Advertise with us
django   0   32587
Hello Word in Django 2: How to start with Django 2

We have already covered how to start with Django 1.9. In this article, we will see how to quickly get a simple page running using Django 2.2.


Create a virtual environment using python3. 

virtualenv -p /use/bin/python3.5 venv

Activate the virtual environment.

source venv/bin/activate

Now install the Django2.2 inside this virtual environment using pip.

pip install Django==2.2

Once Django has been installed, run the django-admin command to start a project.

Create your project.

$ django-admin startproject helloworld

Go inside the newly created project directory. 

$ cd helloworld

You will see a file  and a directory with the same name as of your project.

Create an app here. Every project consists of one or more apps. These are plug-able modules that can be reused in another project if written properly. 

$ python startapp hw

Go inside hw directory. create a new file

Add the below lines to this file and save it. 

from django.urls import path

from . import views

app_name = "hw"
urlpatterns = [
path(r'', views.home, name='home'),

    Open file and save the below code in it.  This will create the first view function which will be called when hitting base project URL i.e. localhost:8000/ on the local server.

    from django.shortcuts import render

    # Create your views here.
    def home(request):
    data = dict()
    return render(request, 'hw/home.html', data)

    Include the hw app's URLs in the main project URLs file.

    Open helloworld/ file and add below line in urlpatterns.

    from django.contrib import admin
    from django.urls import path, include

    urlpatterns = [
    path(r'', include('hw.urls', namespace="hw")),

    Now finally add hw in installed apps in helloworld/ file.


    Now start the Django development server by running the command. 

    $ python runserver

    This will run the python HTTP server on the localhost and 8000 port. If you want to run it on a different port, use port number in the command. For example $ python runserver 8888.

    Use the below command to make your project available for everyone on the network.

    $ python runserver

    Open the web browser and got to URL localhost:8000 and you will be able to see the home page.

    django 404 not found

    Source code:

    The source code is available on GitHub.

    Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.

    django   0   32587

    Related Articles:
    How to reset Django superuser password
    This article explains 3 methods to reset the user password in Django, What command should be used to reset the superuser password from the terminal in Django application, Changing the user password in Django...
    Encryption-Decryption in Python Django
    How to encrypt and decrypt the content in Django, Encrypting the critical information in Django App, Encrypting username, email and password in Django, Django security...
    How to upload an Image file in Django
    This article explains the simple steps of uploading and storing an image in Django application, After storing the image, how to use it in Django template or emails, Uploading a file in Django, Storing image in Django model, Uploading and storing the image in Django model, HTML for multipart file upload...
    Getting query params from request in Django
    In this article, we will see how to access the query parameters from a request in the Django view, Accessing GET attribute of request, get() vs getlist() method of request in Django, query parameters Django,...

    0 thoughts on 'Hello Word In Django 2: How To Start With Django 2'
    Leave a comment:

    *All Fields are mandatory. **Email Id will not be published publicly.

    DigitalOcean Referral Badge

    Get a .COM for just $5.98!

    © 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools