Django Development 2

Django

Django is an open source web application framework written in Python. It is currently the most popular web development framework using the Python language. The domestic Douban network and the fruit shell network all use the Django framework. The main purpose of Django is to develop database-driven websites easily and quickly.

Install Django
$ sudo pip install django
$ sudo pip install django==1.10 #指定版本号
Create Django project
$ django-admin startproject mysite

Project directory is as follows:

mysite/
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
  • manage.py : Command line tools for managing projects, such as starting the server, generating the database, etc. Use it to complete.
  • mysite: : The actual python module directory of the project.
  • settings.py : The project configuration file.
  • urls.py : URL mapping configuration file.
  • wsgi.py :django can Use other web servers like apache. WSGI is the interface between the web server and our django application.

can start the server by command:

$ python manage.py runserver
$ python manage.py runserver 8000 #specport
$ python manage.py runserver 0.0.0.0:8000 #指定ip and port
Create an application:

A django project can be divided into multiple applications based on functionality, such as a website with news content, or a blog . Then you can create two apps news and blog.

$ python manage.py startapp blog

The contents of the application directory are as follows:

blog/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py
View The view

view file places the python functions we write, which are the logic to respond to a client's http request. For example, if the client visits http://www.xxx.com/home, we will process the logic of this request (return a page) in a view function.

blog/views.py :

from django.http import HttpResponse

def blogPage(request):
    html = '<html><body>Welcom to my blog.</body></html>'
    return HttpResponse(html)
URLConfigure

client access url Which specific view function to handle, we need to configure in urls.py. mysite/urls.py :

from django.conf.urls import url
from blog.views import blogPage

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog', blogPage),
]

mysite/urls.py is the total entry, you can also level one level routing, we create a urls.py under the blog, put An application url mapping is configured in the application's urls file.

blog/urls.py :

from django.conf.urls import url
from . import views

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

mysite/urls.py :

from django.conf.urls import url, include
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/', include('blog.urls')),
]
模型 model

blog/model.py :

class Blog(models.Model):
    title = models.CharField(max_length=150)
    body  = models.TextField()
    timestamp = models.DateTimeField()

django creates a table for us in the database based on the class object defined in the models file. Django can be easily configured to use a variety of databases, using SQLite by default. It can be configured by editing settings.py.

mysite/settings.py

DATABASES = {
     'default': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
     }
}