In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Django is an efficient web development framework, and Django can be used to build and maintain high-quality web applications with minimal cost. By reducing repetitive code, it allows people to focus on interesting and critical things in web applications.
The model-view-controller (MVC) design pattern is followed. To put it simply, MVC is a method of software development that separates the code that defines and accesses data (model model), the code that controls request logic (controller controller), and the user interface (view view). We will discuss MVC in more depth in the future.
The most important advantage of this approach is that it is loosely coupled (loosely coupled). In this way, each part of a Web program developed in Django has its own single purpose and can be modified separately without affecting other parts. For example, a programmer can modify the URL; without affecting the underlying implementation. The HTML; database administrator of the page can modify the page without touching the Python code. The HTML; database administrator only needs to change one place after renaming the data table, instead of looking for replacements in a lot of files.
one。 The Construction of Django basic Environment
Because the development direction set by the author is operation and maintenance development, all the development work will be carried out on the linux platform, and the subsequent environment construction is basically in the linux system, and the system used by the author is RHEL7.2. Different virtual environments can be managed through pyenv, and different versions of python can be used in different virtual environments, which provides convenience for subsequent project development.
1. Install pip
Yum install python-pip
two。 Install pyenv
The process of installing pyenv is long, see the previous python development environment preparation article.
3. Create a virtual environment using pyenv, and then switch to the virtual environment
Pyenv virtualenv 3.5.2 myprojectpyenv local myproject
4. Use pip to install django in a virtual environment
(myproject) # pip install django
5. Check to see if django is installed successfully
# python-m django-- version
You can see the following screen, which shows that django has been installed successfully. The django version installed here is 1.9.8, which is the latest version.
II. Basic commands of Django
First I create a directory of Django, myproject, and then store my own Django project in this directory.
# mkdir myproject
1. Create a project
# django-admin.py startproject project-name # django-admin.py startproject mywebsite # I want to create my own blog site, the project is called mywebsite
two。 Create an APP in the project
# cd mywebsite # go to the mywebsite project directory # project manage.py startapp app-name # this is the default command to create a website APP
# is equivalent to creating a sub-project under the project
# the first version of the website, called myweb# python manage.py startapp myweb
3. Synchronize database
# python manage.py makemigrations # synchronize database # python manage.py migrate # create database # python manage.py flush # empty the database and leave an empty table (note when using it) # python manage.py dbshell # enter the database command line and ask for a user name and password if it is mysql or postresql
We just started to create a website, we just need to create a basic database, and the rest of the commands just need to be memorized.
$python manage.py migrate
4. Start your own website and use the Django initial page
# Command description
# python manage.py runserver # default startup mode, listening on port 127.0.0.1 IP 8000 # python manage.py runserver ip:port # listening on other IP # execute command # python manage.py runserver 127.0.0.1 IP 8000
Then open the browser, open the http://127.0.0.1:8000 page, and if you see the following page, your first step has been successful.
5. More commands
Enter the python manage.py command to view a detailed list of commands, which can be queried when you don't remember the relevant commands.
III. A brief introduction to the Django framework
After creating the Django project mywebsite and its subordinate sub-project myweb, the file directory structure of the project is as follows:
# tree mywebsitemywebsite/ ├── manage.py └── mywebsite ├── _ _ init__.py ├── settings.py ├── urls.py └── wsgi.py
The manag.py file is the entrance to all the management commands of the project, and you can use python manage.py to view all the commands
All the files of the project are in the mywebsite directory.
Initialization file of _ _ init__.py project
Settings.py Django settings file, such as DEBBUG switch, static file location, etc.
The entry of the urls.py URL, associated with a function in views.py (or generic class), and a function corresponding to visiting a URL.
Files used by wsgi.py to deploy the server
The directory structure of the subproject myweb is as follows:
# tree mywebmyweb ├── admin.py ├── apps.py ├── _ _ init__.py ├── migrations │ └── _ _ init__.py ├── models.py ├── tests.py └── views.py
Admin.py files related to background login
Files related to apps.py myweb attributes
Mirgrations database directory
Files related to models.py database operations
Documents related to tests.py project testing
Views.py URL function, which corresponds to the urls.py in the project directory
Start the default interface of Django
1. Add the newly defined myweb to the INSTALL_APPS in settings.py
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','myweb', # new content]
Also add the foreign network address of the Nic to be monitored to the trusted address list ALLOWED_HOSTS in settings.py (what's new in Django1.10):
ALLOWED_HOSTS = ['192.168.1.246,]
two。 Define the view function (the content when accessing the page) in the views.py file in myweb
# vim myweb/views.py
# the following is what is added to the views.py file
# coding:utf-8from django.http import HttpResponsedef index (request): return HttpResponse ("Welcome to Little Grey's first station")
The first line declares the utf-8 code, because Chinese is used below, and an error will be reported if you do not use utf-8.
The second line inserts a HttpResponse class, which is used to return content to the page and display the content on the page.
The third line defines a function index (request). The first parameter is request, which is related to the request from the web page. The request variable contains the content of get or post, and the user's browser and system information is included. The function returns a HttpResponse object that displays the value contained above.
3. Add the URL corresponding to the view function in the urls.py file we mentioned in the third part above.
# vim mywebsite/urls.pyfrom django.conf.urls import urlfrom django.contrib import adminfrom myweb import views as mywb_views # newly added, insert views module urlpatterns = [url (r'^ $', myweb_views.index) from the myweb project, # URL corresponds to the index function url (r'^ admin/', admin.site.urls) defined in the views module,]
Save the content after modification
4. Start our first Django project, because it is operated on a virtual machine, so we need to listen on the external network card IP
# python manage.py runserver 0.0.0.0:8000
Then open a browser on another machine, enter the virtual machine's IP and port http://192.168.1.246:8000, and you can see the following page, indicating that our first web site was created successfully.
The knowledge points of this chapter:
1. Domestic source configuration of pip
two。 Configuration of virtual development environment
3. Basic commands of Django, including project, APP creation, data creation and update.
Django-admin.py startproject project-name
Project manage.py startapp app-name
Python manage.py makemigrations # synchronize the database
Python manage.py migrate # create a database
Python manage.py flush # empty the database and leave the empty table (be careful when using it)
Python manage.py dbshell # enter the database command line and ask for a user name and password if it is mysql or postresql
Python manage.py runserver # starts by default and listens on port 127.0.0.1 on port 8000
Python manage.py runserver ip:port # listening on other IP
4. The basic structure of Django project, the meaning and relationship of views.py, setting.py, urls.py and admin.py.
Views.py defines the url response function, which is used with urls.py.
Settings.py defines some basic configuration parameters
Urls.py defines the corresponding url rules
Admin.py corresponds to management backend
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 273
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.