Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Web based on Python+Django+Jquery Architecture

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to carry out Web development based on Python+Django+Jquery architecture. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Recently, the company is going to develop an automated operation and maintenance platform, using Web development based on Python+Django+Jquery architecture. Here is an introduction to the construction of this architecture.

Environmental Information:

Operating system: Linux rhel 7.1

Database: mysql 5.6

Architecture software that needs to be installed:

[root@centos7mysql django] # ll

Total 34336

-rw-r--r-- 1 root root 7989435 Jun 27 18:06 Django-2.0.6.tar.gz

-rw-r--r-- 1 root root 1081874 Apr 21 2016 pip-1.5.4.tar.gz

-rw-r--r-- 1 root root 74297 Jun 27 22:56 PyMySQL-0.8.1.tar.gz

-rw-r--r-- 1 root root 22994617 Jun 27 18:10 Python-3.6.5.tgz

-rw-r--r-- 1 root root 308066 Jun 27 18:04 pytz-2018.4.tar.gz

-rw-r--r-- 1 root root 2699252 Jun 27 17:22 sqlite-autoconf-3240000.tar.gz

[root@centos7mysql django] #

Installation of architecture software

Step 1: install SQLITE 3

After decompressing, go to the decompressed directory of sqlite and compile:

$configure-prefix= # # here I set / usr/local/sqlite

$make-j24

$make install

Step 2: install sqlite-devel

Check to see if the package is installed

Rpm-qa | grep-I sqlite

If not, install using the yum source

Yum install sqlite-devel

This step is very important, otherwise Python cannot be loaded into Sqlite3.

Report a similar error below

1) ImportError: dynamic module does not define module export function (PyInit__sqlite3)

2) No module named _ sqlite3

Step 3: install Python 3

After decompressing Python-3.6.5.tgz, enter Python-3.6.5

. / configure-- prefix=/usr/local/python3

Make & & make install

Python 3.6.5 automatically installs setuptools and pip, with versions of pip-9.0.3 setuptools-39.0.1

Set environment variabl

Echo 'export PATH=$PATH:/usr/local/python3/bin' > > ~ / .bashrc

Vi modifies the configuration of yum

#! / usr/bin/python changed to #! / usr/bin/python2.7

Step 4: install pytz

After decompressing pytz, enter pytz-2018

Python3 setup.py install

Step 5: install Django

After decompressing django, enter Django-2.0.6

Python3 setup.py install

Step 6: verify the installation success

New project

Django-admin startproject testDJ

Generate the Project directory testDJ under the current directory

Start the server

Python3 manage.py runserver

Create an application APP

Python manage.py startapp EAOPS

DJANGO application architecture file structure

[root@centos7mysql ~] # tree. / testDJ/

. / testDJ/ # root directory of the project

Project test database sqllite for ├── db.sqlite3 # Django

Application directory of ├── EAOPS # Django

│ ├── admin.py

│ ├── apps.py

│ ├── _ _ init__.py

│ ├── migrations

│ │ ├── _ _ init__.py

│ │ └── _ _ pycache__

│ │ └── _ _ init__.cpython-36.pyc

│ ├── models.py # applies EAOPS's database template configuration file, which is equivalent to SSH's entity class file

Backup files for │ ├── models.py.bak # template files

│ ├── _ _ pycache__

│ │ ├── admin.cpython-36.pyc

│ │ ├── _ _ init__.cpython-36.pyc

│ │ ├── models.cpython-36.pyc

│ │ └── views.cpython-36.pyc

│ ├── tests.py

│ └── views.py # applies the view configuration file of EAOPS, which is equivalent to the controller file of SSH

Management files for ├── manage.py # django

The storage path of static files such as js, css, pictures and so on in the application of ├── static # django of APP

│ ├── js

│ │ ├── jquery-1.4.3.min.js

│ │ └── jquery-1.8.0.js

│ └── logo # the Logo file storage path of the application EAOPS

│ └── favicon.ico

├── templates # applies the template configuration file of EAOPS, which is equivalent to SSH's jsp or html file

│ ├── index.html

│ └── login.html

Configuration file storage path for └── testDJ # project

├── _ _ init__.py

├── _ _ pycache__

│ ├── _ _ init__.cpython-36.pyc

│ ├── settings.cpython-36.pyc

│ ├── urls.cpython-36.pyc

│ └── wsgi.cpython-36.pyc

The core configuration file of the ├── settings.py # project includes the mapping path of the project application, database configuration, and static file mapping path.

Web request profile for the ├── urls.py # project, which is equivalent to the struts.xml profile for SSH

└── wsgi.py

10 directories, 28 files

[root@centos7mysql ~] #

Apply the mapping path configuration of template files in settings.py

TEMPLATES = [

{

'BACKEND': 'django.template.backends.django.DjangoTemplates'

'DIRS': ['/ root/testDJ/templates',]

'APP_DIRS': True

'OPTIONS': {

'context_processors': [

'django.template.context_processors.debug'

'django.template.context_processors.request'

'django.contrib.auth.context_processors.auth'

'django.contrib.messages.context_processors.messages'

]

}

}

]

Apply the configuration of database in settings.py

DATABASES = {

'default': {

# 'ENGINE':' django.db.backends.sqlite3'

# 'NAME': os.path.join (BASE_DIR,' db.sqlite3')

'ENGINE': 'django.db.backends.mysql'

'NAME': 'opsdb'

'USER':'root'

'PASSWORD':'rootroot'

'HOST':'127.0.0.1'

'POST':'3306'

}

}

Apply the mapping path of js, css and picture in settings.py

STATIC_URL ='/ static/'

STATICFILES_DIRS = (

Os.path.join (BASE_DIR, 'static')

)

Apply the configuration of the urls.py file

From django.contrib import admin

From django.urls import path

From django.conf.urls import url

From EAOPS import views

From EAOPS.models import *

From django.views.generic.base import RedirectView

Urlpatterns = [

# path ('admin/', admin.site.urls)

Url (r'^ favicon.ico$',RedirectView.as_view (url=r'static/logo/favicon.ico')), # apply the logo configuration path of EAOPS

Url (r'^ login/$',views.Login), # apply the configuration of http request login action

Url (r'^ UserLogin/$',views.UserLogin), # apply the configuration of jquery request actions

Url (r'^ Index/$',views.Index), # the Index action request for applying EAOPS is the redirection of jquery's UserLogin

]

Model profile that applies EAOPS

[root@centos7mysql EAOPS] # cat models.py

#-*-coding:utf-8-*-

From _ _ future__ import unicode_literals

From django.db import models

# Create your models here.

Class USER (models.Model):

Userid = models.AutoField (primary_key=True)

Username = models.CharField (max_length=30)

Pwd = models.CharField (max_length=30)

Status = models.SmallIntegerField ()

[root@centos7mysql EAOPS] #

Apply the view of EAOPS, which is equivalent to the implementation of controller file of SSH

[root@centos7mysql EAOPS] # cat views.py

From django.shortcuts import render

From django.http import HttpResponse

From django.shortcuts import render_to_response

From django.http import HttpResponseRedirect

From django.views.decorators.csrf import csrf_exempt

Import time, datetime

Import json

From django.db import connection,transaction

From django.template import RequestContext

From EAOPS import models

From EAOPS.models import *

From django.db.models import Q

From django.core import serializers

# Create your views here.

Def Login (request): # implementation of urls.py request Login action

Return render (request,'login.html')

Def Index (request): # implementation of urls.py request Index action

Return render (request,'index.html')

@ csrf_exempt

Def UserLogin (request): # implementation of urls.py request UserLogin action

Username = request.POST.get ('username') # receive the username parameter of the AJAX

Password = request.POST.get ('pwd') # receive the password parameter of the AJAX

Users of djuser=USER.objects.filter (Q (username=username) & Q (pwd=password)) # django log in to ya

Data = {}

Response = HttpResponse ()

Response ['Context-Type'] = "text/javascript"

If len (djuser.values ()):

Data = juser.values () [0]

Rdata = json.dumps (data)

Else:

Data = {"msg": "failed"}

Rdata = json.dumps (data)

Response.write (rdata)

Return response

[root@centos7mysql EAOPS] #

Apply the login.html file of EAOPS

[root@centos7mysql templates] # cat login.html

EAOPS- operation and maintenance automation platform

Function login () {# user login page login button action jquery method implementation

Var username=$ ('# username') .val ()

Var pwd=$ ('# password') .val ()

Alert (username+''+ pwd)

Var args = {"username": username, "pwd": pwd}

Var url = "/ UserLogin/"

$.ajax ({# ajax request action

Url:url

Data:args

Type: "POST"

DataType:'json'

Success:function (res) {

Var obj = eval (res)

If (! obj.hasOwnProperty ("msg")) {

$('# tip') .html ("login successfully!")

_ window.location.replace ("/ Index")

} else {

$('# tip') .html ("account or password is wrong! try agin please!")

}

}

});

}

EAOPS, welcome!

Administrator login

User name:

Password:

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report