Django: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 61: Line 61:


= Creating an App =
= Creating an App =

{{UC}}
cd mytool
python3 manage.py startapp myapp

Edit the installdir/apps/django/django_projects/PROJECT/APP/views.py file and add this content:

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello world!")

Create the installdir/apps/django/django_projects/PROJECT/APP/urls.py file and add these lines to it:

from django.conf.urls import url

from . import views

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

Edit the installdir/apps/django/django_projects/PROJECT/PROJECT/urls.py 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')),
]


http://10.140.197.8:8080/myapp/

<br />
<br />
<br />
<br />

Revision as of 22:03, 4 June 2019


Source: djangoproject.com

Installing

Installation:

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:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py


These files are:

  • The outer mysite/: Just a container for the project. It can be renamed it to anything.
  • manage.py: 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/__init__.py: Empty file that tells Python that this directory should be considered a Python package.
  • mysite/settings.py: Settings/configuration for this Django project.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

Running Project

python3 manage.py runserver
http://127.0.0.1:8000/

Changing the port

python3 manage.py runserver 8080

Listen on all available public IPs:

python3 manage.py runserver 0:8000
python3 manage.py runserver 0.0.0.0:8000

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 198.211.99.20 to ALLOWED_HOSTS setting in project settings.py file
ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

Creating an App

cd mytool
python3 manage.py startapp myapp

Edit the installdir/apps/django/django_projects/PROJECT/APP/views.py file and add this content:

from django.http import HttpResponse
def index(request):
  return HttpResponse("Hello world!")

Create the installdir/apps/django/django_projects/PROJECT/APP/urls.py file and add these lines to it:

from django.conf.urls import url
from . import views
urlpatterns = [
  url(r'^$', views.index, name='index'),
]

Edit the installdir/apps/django/django_projects/PROJECT/PROJECT/urls.py 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')),
]


http://10.140.197.8:8080/myapp/





References





{{#widget:DISQUS |id=networkm |uniqid=Django |url=https://aman.awiki.org/wiki/Django }}