Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
Django   0   12342
Hello Word in Django: How to start with Django

In this article, we will learn how to develop and run a python-Django app in less than 5 minutes.

Prerequisite: Python3.


Step to create and run the Django app:

Install the virtual environment. You may proceed without a virtual environment too, but in the long run, the virtual environment is going to be very helpful.

$ pip install virtualenv


Create a virtual environment.

$ virtualenv -p /usr/bin/python3 helloworld_VE

Activate the virtual environment.

$ source helloworld_VE/bin/activate

Install the latest Django framework in the virtual environment.

$ pip install django

Create your project.

$ django-admin startproject myproject

Go inside the newly created project directory. 

$ cd myproject

You will see a file manage.py  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 manage.py startapp helloworld

Go inside helloworld  directory. create a new file urls.py.

Add the below lines to this file and save it.

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]


Open views file and save the below code in it. 

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello World. First Django Project. PythonCircle.Com")


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

Open myproject/urls.py file and make add below line in urlpatterns.

url(r'^helloworld/',include('helloworld.urls')),


Also, import include module.

from django.conf.urls import include

Now finally add helloworld in installed apps in myproject/settings.py file.


Now start the Django development server by running the command. 

$ python manage.py 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 manage.py runserver 8888.

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

$ python manage.py runserver 0.0.0.0:8000


So this was a basic tutorial to set up a Django app in less than 5 minutes. You can refer to GitHub code and video.   

Code on Github:
Github URL : https://github.com/anuragrana/hellow-world-django.git   

Video:



Hosting the Django app on the server:

Read how you can host your Django app on pythonanywhere server for free. A complete step by step guide with all screenshots and video.


Django   0   12342
0 comments on 'Hello Word In Django: How To Start With Django'
Login to comment


Related Articles:
How to start logging errors in Django app
How to debug Django code in production environment? How to start logging errors in log files in django. Error loggers in django web application....
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap