Python2.7 + Django1.8 + Bootstrap3 for addition, deletion, modification, and query, paging (1), and django1.8bootstrap
Preface
I haven't written a blog for a long time. This time, for work reasons, I implemented a test demo Based on Python2.7 + Django1.8 + Bootstrap3 architecture. In addition to the common addition, deletion, modification, and query functions, the paging and batch operations functions are also added, from the front-end to the back-end. The underlying database uses the SQLite lightweight database that comes with Django, And I will install and run it later, the principle and framework of implementation, as well as my own experiences, will tell you how this demo was developed. I am very user-friendly, and the source code will be provided in the tail, although the level is not good, however, if it is reproduced, please indicate the source. Of course, I will not reserve any rights if it is not specified.
Installation and configuration
Install Python2.7 first. During the next step, check the environment configuration toolkit. It is disabled by default. If not set, manually add "... /Python27 "and "... /Python27/Scripts "to the path of the environment variable. If the configuration is successful, Enter cmd and enter Python in any path to view the Python version information. In general "... /Python27/Scripts "contains the following execution text:
django-admin.exeeasy_install.exepip.exe
Input pip in cmd terminal, in general there will be a reaction, if there is no, that is not integrated criticism, you can directly download: get-pip.py and then run in terminal run
python get-pip.py
You can install pip. After pip is successfully installed, you can use pip to install Django.
pip install Django
Input in the python terminal environment
import djangodjango.get_version()
View the output Django version information. If printed, the installation is successful!
Implementation and principles
After Python is integrated with the Django development framework, you can create a project at the cmd command prompt. The project name is learn_models.
django-admin.py startproject learn_models
Go to learn_models and create an app project.
cd learn_modelspython manage.py startapp learn
The directory structure contains these files.
C:\USERS\SHILEIDING\LEARN_MODELS│ manage.py│ ├─learn│ │ admin.py│ │ models.py│ │ tests.py│ │ views.py│ │ __init__.py│ │ │ └─migrations│ __init__.py│ └─learn_models settings.py settings.pyc urls.py wsgi.py __init__.py __init__.pyc
Go to the official website to download the latest Bootstrap3 framework file http://getbootstrap.com/getting-started/#download download folder can be seen in css, fonts, js three (function is quite large), This Is All Bootstrap 3, in the following example, you need to go to the learn_models directory in the newly created Django project collection Bootstrap3, create a static folder, create a bootstrap folder in static, and put the three downloaded folders in.
References: Reference relevant libraries such as Bootstrap and jQuery. The focus here is to locate the static files that are stored.
<! DOCTYPE html >{% load staticfiles %}< html> </Html>
The {% load staticfiles %} at the beginning of the file is to load the static directory. To find the static directory, You need to modify it slightly "... /learn_models/settings. in py, there are two major modifications
INSTALLED_APPS = ('django. contrib. admin', 'django. contrib. auth ', 'django. contrib. contenttypes ', 'django. contrib. sessions ', 'django. contrib. messages ', 'django. contrib. staticfiles ', # register the newly created app 'learn ',)
Add the newly created app to INSTALLED_APPS and configure static
STATIC_URL = '/static/'STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Put the static directory in STATICFILES_DIRS so that you can load it to the newly downloaded bootstrap. bootstrap depends on the jQuery library, so you must add it. We will directly reference it here, if you have a downloaded version, you only need to put it in static and then reference it.
At this time, the front-end html can already use the relevant bootstrap attributes, but how to access it through Django's http protocol? This is the legendary MVC model of Django. The templates folder is the presentation layer, showing the front-end views to users. py is responsible for processing the business logic layer, processing requests and return requests, models. py is responsible for the data access layer to process database attributes. The GET or POST request sent by the front-end must pass the URL. py maps to the related methods of views, so in urls. configure the ing in py. Assume that the Request Path is http: // 127.0.0.1: 8000/test /.
Urlpatterns = [url (R' ^ admin/', include (admin. site. urls), # The first is the regular expression url (R' ^ test/', 'learn. views. test', name = 'test'),]
Maps to the corresponding views. py. Here, the test method is implemented simply, and you can add it in views. py.
# Bootstrap test def test (request): return render (request, 'test.html ')
When the browser sends a request for "test", it first passes through the urls to the test.pdf in views, and the processing logic is pushed to the frontend test.html for display. The content displayed in html can be rendered using the downloaded bootstrap.
Run
Cd to the learn_models directory in cmd. The directory structure is as follows:
C:\USERS\SHILEIDING\LEARN_MODELS│ manage.py│ ├─learn│ │ admin.py│ │ models.py│ │ tests.py│ │ views.py│ │ __init__.py│ │ │ ├─migrations│ │ __init__.py│ │ │ └─templates│ test.html│ ├─learn_models│ settings.py│ settings.pyc│ urls.py│ wsgi.py│ __init__.py│ __init__.pyc│ └─static └─bootstrap ├─css │ bootstrap-theme.css │ bootstrap-theme.css.map │ bootstrap-theme.min.css │ bootstrap.css │ bootstrap.css.map │ bootstrap.min.css │ ├─fonts │ glyphicons-halflings-regular.eot │ glyphicons-halflings-regular.svg │ glyphicons-halflings-regular.ttf │ glyphicons-halflings-regular.woff │ glyphicons-halflings-regular.woff2 │ └─js bootstrap.js bootstrap.min.js npm.js
You can see manage. py, which is the running manager. Synchronize the database and then run the project.
# Synchronize database python manage. py makemigrationspython manage. py migrate # Run the project python manage. py runserver
Start http: // 127.0.0.1: 8000/test/and the "hello world" in the middle of the page indicates that the integration is successful.
Tail
I was going to explain the basic operation demo of table management directly, but I think it took a lot of detours to configure this layer at the beginning, I am writing a test project that integrates Python, Django, and bootstrap with a slight explanation. I will write another article to describe the blog that implements basic operations under the Django framework, here, I have contributed the demo source code. I can directly synchronize the database and run it. If you are interested, I can directly read the notes in it. If you are patient, I will break it down next time!