From Network Security Wiki
Jump to navigation Jump to search




sudo pip3 install django

Check installed version:

python3 -m django --version

Creating project

cd  /home/mycode                  #Do not put code under /var/www
django-admin startproject mysite 

Avoid using names like

django: conflict with Django itself
test:   conflicts with a built-in Python package

Startproject command will create:


These files are:

  • The outer mysite/: Just a container for the project. It can be renamed it to anything.
  • Command-line utility that allows to interact with this Django project in various ways.
  • The inner mysite/ directory is the actual Python package for the project. This is the Python package name to use to import anything inside it (e.g. mysite.urls).
  • mysite/ Empty file that tells Python that this directory should be considered a Python package.
  • mysite/ Settings/configuration for this Django project.
  • mysite/ The URL declarations for this Django project; a “table of contents” of your Django-powered site.
  • mysite/ An entry-point for WSGI-compatible web servers to serve your project.

Running Project

python3 runserver

Changing the port

python3 runserver 8080

Listen on all available public IPs:

python3 runserver 0:8000
python3 runserver

Automatic reloading of runserver:

The server automatically reloads Python code for each request as needed.
No need to restart the server for code changes to take effect. 
Some actions like adding files don’t trigger a restart, need to restart the server in these cases.

Invalid http_host header

Need to add to ALLOWED_HOSTS setting in project file
ALLOWED_HOSTS = ['', 'localhost', '']

Creating an App

 cd mytool
 python3 startapp myapp
  • This is the simplest view possible in Django.
  • Edit the /mytool/myapp/ file:
 from django.http import HttpResponse

 def index(request):
   return HttpResponse("Hello world!")
  • To call the view, we need to map it to a URL - and for this we need a URLconf.
  • To create a URLconf in the polls directory, create a file called
  • Create the /mytool/myapp/ file:
 from django.conf.urls import url

 from . import views

 urlpatterns = [
   url(r'^$', views.index, name='index'),
  • The next step is to point the root URLconf at the myapp.urls module.
  • In mytool/, add an import for django.urls.include and insert an include() in the urlpatterns list,
  • Edit the /mytool/mytool/ file and modify it to look like this:
 from django.conf.urls import url
 from django.urls import include

 urlpatterns = [
     url(r'^myapp/', include('myapp.urls')),

Test the site:

Deploy Django project

   Paste.png     This section is under construction.


blog comments powered by Disqus