Build a MySQL database supported Web application (Linux) in Django

Source: Internet
Author: User
Tags bz2

Run: Python If the error indicates that the system is not mounted python or you can bypass the installation python step
Install Python
Download wget http://python.org/ftp/python/2.7/Python-2.7.tar.bz2

TAR-JXVF python-2.7.tar.bz2
CD Python-2.7
./configure
Make all
Make install
Installing Django
Download wget https://www.djangoproject.com/m/releases/1.8/Django-1.8.2.tar.gz

Tar xzvf django-1.8.2.tar.gz
CD Django-1.8.2
AddUser python
sudo python setup.py install

Installing Setuptools
Download wget https://enterprise-storage-os.googlecode.com/files/setuptools-0.6c11.tar.gz

Tar xzvf setuptools-0.6c11.tar.gz
CD SETUPTOOLS-0.6C11
Python setup.py Install

Installing Mysql-python
Download wget http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3b1/MySQL-python-1.2.3b1.tar.gz/download

Tar xvzf mysql-python-1.2.3b1.tar.gz
CD mysql-python-1.2.3
Python setup.py Build
sudo python setup.py install

After the installation is complete, you can proceed to the next step.

Create the first Django project and create an application in it
First, you might want to create a folder to store your Django project, and go to this folder from the operating system prompt:
Example: mkdir Django
CD Django
Then issue the following command:
django-admin.py Startproject MyProj

Create an application within this project

chmod +x manage.py
./manage.py Startapp MyApp

May appear:
Raise Improperlyconfigured ("Error loading either Pysqlite2 or Sqlite3 modules (tried in this order):%s"% exc)
Django.core.exceptions.ImproperlyConfigured:Error loading either PYSQLITE2 or Sqlite3 modules (tried in this order):
No module named _sqlite3

Workaround:
VI myproj/settings.py
Find DATABASES
Revision changed to
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ', #设置为mysql数据库
' NAME ': ' Test ', #mysql数据库名
' User ': ' Root ', #mysql用户名, leave blank then default to the current Linux user name
' PASSWORD ': ' 123456 ', #mysql密码
' HOST ': ', #留空默认为localhost
' PORT ': ', #留空默认为3306端口
}
}
Note: The Chinese comments above may cause syntaxerror without specifying the python file encoding, so do not copy these Chinese comments.
This step is equivalent to selecting a Connection object for the database.

VI myproj/urls.py
Add a record to the Urlpatterns:
From MyApp import views as Myapp_views

URL (r ' ^articles/', myapp_views.latest_article),


Add a record to the template:
VI myproj/settings.py

' DIRS ': [Os.path.join (Base_dir, ' templates '),],

Object-relational mapping with models

VI myapp/models.py

Add to
Class student (models. Model):
id = models. Integerfield (Primary_key=true)
Name = models. Charfield (max_length=20, NULL = True)
class = models. Charfield (MAX_LENGTH=25)
Age = Models. Charfield (MAX_LENGTH=25)
Class Meta:
db_table = "Student"

Note there is an error in the class of Python reserved words, you can modify the corresponding field of the database
(Modify method: ALTER TABLE student change class Myls varchar (30);
In the above example, it is important to note the use of the class meta, which provides metadata options for the model. In this example,
You use the Db_table option to explicitly specify the name of the table to which the model is to be mapped.
In fact, by default, the name of the Django hypothesis table consists of the name of the model class and the application name (delimited by an underscore (_) symbol).
So, in this example, Django looks for a table named Myapp_student. Of course, db_table is not the only option you can use in the model's internal class Meta.
You can view a list of available meta options on the Model Meta Options page of the Django document.


VI myapp/views.py

Add to

From Myapp.models import student

def latest_article (Request):
Article_list = student.objects.order_by (' id ')
return render (Request, ' index.html ', {' article_list ': article_list})


mkdir templates
VI index.html

{% for article in article_list%}
author:{{Article.author}}
title:{{Article.title}}
content:{{Article.title}}
</br>
{% ENDFOR%}


Configuration Complete Opens
Http://127.0.0.1:8000/articles

DJANGO1 directory Structure

.
└──myproj
├──index.html
├──manage.py
├──myapp
│├──admin.py
│├──__init__.py
│├──__init__.pyc
│├──migrations
││└──__init__.py
│├──models.py
│├──models.pyc
│├──tests.py
│├──views.py
│└──views.pyc
├──myproj
│├──__init__.py
│├──__init__.pyc
│├──settings.py
│├──settings.pyc
│├──urls.py
│├──urls.pyc
│├──wsgi.py
│└──wsgi.pyc
└──templates
└──index.html

Personal Summary: When we open http://127.0.0.1:8000/articles, the application will first go through the urls.py traversal to find the corresponding view of the method, in the view method through the models to obtain data and upload to the corresponding Web page!

Build a MySQL database supported Web application (Linux) in Django

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.