Difference between revisions of "Django"

From Network Security Wiki
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Scripting]]
 +
__TOC__
 +
<br />
 
Source: [https://docs.djangoproject.com/en/2.1/intro/tutorial01/ djangoproject.com]
 
Source: [https://docs.djangoproject.com/en/2.1/intro/tutorial01/ djangoproject.com]
  
Line 8: Line 11:
 
Check installed version:
 
Check installed version:
 
  python3 -m django --version
 
  python3 -m django --version
 
  
 
= Creating project =
 
= Creating project =
  cd  /home/mycode (Do not put code under /var/www)
+
  cd  /home/mycode                 #Do not put code under /var/www
 
  django-admin startproject mysite  
 
  django-admin startproject mysite  
  
Avoid using names like '''django''' (conflict with Django itself) or '''test''' (conflicts with a built-in Python package)
+
Avoid using names like  
 +
django: conflict with Django itself
 +
testconflicts with a built-in Python package
  
 
Startproject command will create:
 
Startproject command will create:
Line 37: Line 41:
 
= Running Project =
 
= Running Project =
  
  python manage.py runserver
+
  python3 manage.py runserver
 
  http://127.0.0.1:8000/
 
  http://127.0.0.1:8000/
  
 
Changing the port
 
Changing the port
  python manage.py runserver 8080
+
  python3 manage.py runserver 8080
  
 
Listen on all available public IPs:
 
Listen on all available public IPs:
  python manage.py runserver 0:8000
+
  python3 manage.py runserver 0:8000
  python manage.py runserver 0.0.0.0:8000
+
  python3 manage.py runserver 0.0.0.0:8000
  
 
Automatic reloading of runserver:
 
Automatic reloading of runserver:
Line 57: Line 61:
  
 
= Creating an App =
 
= Creating an App =
 +
 +
<syntaxhighlight lang=python>
 +
cd mytool
 +
python3 manage.py startapp myapp
 +
</syntaxhighlight>
 +
 +
*This is the simplest view possible in Django.
 +
*Edit the /mytool/myapp/views.py file:
 +
<syntaxhighlight lang=python>
 +
from django.http import HttpResponse
 +
 +
def index(request):
 +
  return HttpResponse("Hello world!")
 +
</syntaxhighlight>
 +
 +
*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 urls.py.
 +
*Create the /mytool/myapp/urls.py file:
 +
<syntaxhighlight lang=python>
 +
from django.conf.urls import url
 +
 +
from . import views
 +
 +
urlpatterns = [
 +
  url(r'^$', views.index, name='index'),
 +
]
 +
</syntaxhighlight>
 +
 +
*The next step is to point the root URLconf at the myapp.urls module.
 +
*In mytool/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list,
 +
*Edit the /mytool/mytool/urls.py file and modify it to look like this:
 +
<syntaxhighlight lang=python>
 +
from django.conf.urls import url
 +
from django.urls import include
 +
 +
urlpatterns = [
 +
    url(r'^myapp/', include('myapp.urls')),
 +
]
 +
</syntaxhighlight>
 +
 +
Test the site:
 +
http://10.140.197.8:8080/myapp/
 +
 +
= Understanding Parameters =
 +
 +
*The path() function is passed four arguments,
 +
two required: route and view
 +
two optional: kwargs, and name.
 +
 +
*Route:
 +
Route is a string that contains a URL pattern.
 +
When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.
 +
Patterns don’t search GET and POST parameters, or the domain name.
 +
For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/.
 +
In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.
 +
 +
*View:
 +
When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.
 +
 +
*Kwargs:
 +
Arbitrary keyword arguments can be passed in a dictionary to the target view.
 +
 +
*Name:
 +
Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates.
 +
This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.
 +
 +
=  Deploy Django project =
 +
{{UC}}
 +
 +
<br />
 +
<br />
 +
 +
 +
<br />
 +
;References
 +
<references/>
 +
<br />
 +
<br />
 +
<br />
 +
 +
 +
{{DISQUS}}

Latest revision as of 06:15, 5 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
  • This is the simplest view possible in Django.
  • Edit the /mytool/myapp/views.py 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 urls.py.
  • Create the /mytool/myapp/urls.py 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/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list,
  • Edit the /mytool/mytool/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')),
 ]

Test the site:

http://10.140.197.8:8080/myapp/

Understanding Parameters

  • The path() function is passed four arguments,
two required: route and view
two optional: kwargs, and name. 
  • Route:
Route is a string that contains a URL pattern. 
When processing a request, Django starts at the first pattern in urlpatterns and makes its way down the list, comparing the requested URL against each pattern until it finds one that matches.
Patterns don’t search GET and POST parameters, or the domain name. 
For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/. 
In a request to https://www.example.com/myapp/?page=3, the URLconf will also look for myapp/.
  • View:
When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.
  • Kwargs:
Arbitrary keyword arguments can be passed in a dictionary to the target view. 
  • Name:
Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. 
This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.

Deploy Django project

   Paste.png     This section is under construction.





References





blog comments powered by Disqus