Python Django 2



Let’s look at what startproject created:
mysite/
__init__.py
manage.py
settings.py
urls.py

These files are as follows:

· __init__.py: A file required for Python treat the directory as a package (i.e., a group of modules)

· manage.py: A command-line utility that lets you interact with this Django project in various ways

· settings.py: Settings/configuration for this Django project

urls.py: The URL declarations for this Django project; a “table of contents” of your
Django-powered site



A URLconf is like a table of contents for your Django-powered Web site. Basically, it’s a mapping between
URL patterns and the view functions that should be called for those URL patterns. It’s how you tell Django,
“For this URL, call this code, and for that URL, call that code.” Remember that the view functions need to be
on the Python path.


urlpatterns = patterns('',
(r'^time/$', current_datetime),
)

The r in r'^time/$' means that '^time/$ is a Python raw string. This allows regular
expressions to be written without overly verbose escaping.

The caret character (^) and dollar sign character ($) are important. The caret means “require that the
pattern matches the start of the string,” and the dollar sign means “require that the pattern matches the
end of the string.”


Django Processes a Request
when
we run the Django development server and make requests to Web pages:
The command python manage.py runserver imports a file called settings.py from the
same directory. This file contains all sorts of optional configuration for this particular Django
instance, but one of the most important settings is ROOT_URLCONF. The ROOT_URLCONF setting


Example:
manage.py

#!/usr/bin/env python
"""
Command-line utility for administrative tasks.
"""
import os
import sys
if __name__ == "__main__":
    os.environ.setdefault(
        "DJANGO_SETTINGS_MODULE",
        "MVKFarming.settings"
    )
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)









tells Django which Python module should be used as the URLconf for this Web site.
Remember when django-admin.py startproject created the files settings.py and
urls.py? Well, the autogenerated settings.py has a ROOT_URLCONF that points to the
autogenerated urls.py. Convenient.




ROOT_URLCONF = 'MVKFarming.urls'

·
When a request comes in—say, a request to the URL /time/—Django loads the URLconf pointed
to by the ROOT_URLCONF setting. Then it checks each of the URLpatterns in that URLconf in order,

comparing the requested URL with the patterns one at a time, until it finds one that matches. When it
finds one that matches, it calls the view function associated with that pattern, passing an
HttpRequest object as the first parameter to the function. (More on HttpRequest later.)
·
· The view function is responsible for returning an HttpResponse object.






urlpatterns = [
    # Examples:
    url(r'^$', app.views.home, name='home'),
    url(r'^contact$', app.views.contact, name='contact'),
    url(r'^about', app.views.about, name='about'),
    url(r'^farms', app.views.farms, name='farms')]








Comments

Popular posts from this blog

Opencv Python

OpenCv Python Image process: Image