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

Basic learning of Django framework

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Django installation

Python download address http://www.python.org/download/releases/3.3.4/

Download address of Django: https://www.djangoproject.com/download/

1) install (enter the unzipped directory)

Python setup.py install

2. Configure environment variables (PATH)

Clux Unix Python 27 apprentice LibUnix sitelypackagespacks Djangoth Cposition Python27 Accord Scripts

3. To check whether the installation is successful, you can enter the Django-1.7.11 directory under dos to view the Django version.

1. Enter python

two。 Enter import django

3. Enter django.get_version ()

Create a Django project

Reference tutorial http://www.cnblogs.com/qinjiting/p/4678893.html

Django common commands

1) create a Django project (enter the project path)

Django-admin startproject mysite

Resolution of the created directory

1mysite/ # mysite package, a configuration file for the entire program

2 _ _ init__.py

Configuration file for settings.py # Django

3 url mapping of urls.py # router

4 wsgi.py # python upgrade server module, encapsulated an interface

5manage.py # startup file, startup socker server, program management entry

2) run the Django program (enter the project file)

Python manage.py runserver or default port 8000

Python manage.py runserver 0.0.0.0:9000

Configure in pycharm

Run-> edit configurations

Runserver 0.0.0.0:8000

3) create an app (an app is a website)

Python manage.py startapp web

The contents of the created directory are resolved:

1web/

2 _ _ init__.py

Background management provided by admin.py # Django

3 models.py # things related to database operation

4 tests.py # for testing

5. Processing of views.py # logic

Django is MTV mode, and MTV is a mode of standard operation.

MTV parsing

M: Model # database related things are put in Model

T:template # stores things related to html templates. Template needs to build its own. Template can build a telplate folder in the outermost layer, or a telplate folder in an app. All app in the outermost layer share a telplate folder.

Processing of v:views # logic

Revealing the secrets of Diango framework

1. Create an app

2. Url mapping

3. Functions that deal with url

4. Run the django program

5. Visit url

Diango routing system

1. Distribute mysite\ urls.py in the global url

Url-- > urls.py file

#! / usr/bin/env python

# coding:utf-8

From django.conf.urls import patterns, include, url

From django.contrib import admin

Admin.autodiscover ()

# def active (request):

# pass

Urlpatterns = patterns (''

# Examples:

# url (r'^ $', 'mysite.views.home', name='home')

# url (r'^ blog/', include ('blog.urls'))

Url (r'^ admin/', include (admin.site.urls))

Url (r'^ web/', include ('web.urls'))

# url (r'^', active), # all requests are handed over to the active function for processing

)

2'web.urls for configuration

#! / usr/bin/env python

# coding:utf-8

From django.conf.urls import patterns, include, url

From django.contrib import admin

Admin.autodiscover ()

From web.views import wsyht,list,login

Urlpatterns = patterns (''

# Examples:

# url (r'^ $', 'mysite.views.home', name='home')

# url (r'^ blog/', include ('blog.urls'))

Url (r'^ wsyht/', wsyht)

Url (r'^ list/ (\ d *) / (\ d *) /', list)

Url (r'^ login/ (? P\ d *) / (? P\ w*) /', login), # regular expressions d numbers, w letters and numbers, where jenkins is written, then jenkins is also written for passing parameters there.

Url (r'^ login/ (? P\ d *) /', login, {'doc':333}). If the second parameter is not added after #, 333 will be output by default.

)

3. Views module configuration (logic processing module, request function processing)

#! / usr/bin/env python

# coding:utf-8

From django.shortcuts import render

From django.http.response import HttpResponse

# Create your views here.

Def wsyht (request): # must have request

Return HttpResponse ('index')

Def list (request,id1,id2): # must have request

Print id1,id2

Return HttpResponse ('list')

Def login (request,doc,jenkins): # must have request

Print doc,jenkins

Return HttpResponse ('login')

Create a database table

1. Create a database in the database

2. Configuration file settings connects to the database

Line 39 add app. Let's assume the name of app is web.

Web

Paste the following to replace line 59

DATABASES = {

'default': {

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

'NAME':'dbname'

'USER': 'root'

'PASSWORD': 'xxx'

'HOST':''

'PORT':''

}

}

3. Create a model, which inherits from the models.Model class

Operation under web app

Web.models

Static field =

Username = models.CharField (max_length=50)

Example:

From django.db import models

# Create your models here.

Class UserInfo (models.Model):

Username = models.CharField (max_length=50)

Password = models.CharField (max_length=50) # string

Gender = models.BooleanField (default = False) # Boolean type

Age = models.IntegerField (default = 26) # Integer

Memo = models.TextField (default = 'xxx') # long string

CreateDate = models.DateTimeField (default = '2016-9-24 12 default 12') # date type

# default

4. The practice on the terminal

Synchronize database

Python manage.py syncdb

Note: Django 1.7.1 and above requires the following command

First execute python manage.py makemigrations

Execute python manage.py migrate again

Select 1 to enter a field value

Note: to modify the existing models, the Django version before Django 1.7 cannot automatically change the table structure, but there is a third-party tool south. For more information, please see the Django database migration section.

5. Modify model

6. Perform step 4 again

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

Database

Wechat

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

12
Report