Python's web framework Django

Source: Internet
Author: User
Tags sqlite database install django

  Python's web framework:

Django

One. Django

Django is an excellent next-generation web framework

                  Django's processing flow

  

  1.   

python:https://www.python.org/downloads/

django:https://www.djangoproject.com/download/

Note: Before installing Django, you must install Python first.

 2. Installation

    

Install under Windows: (Use Pycharm here)

>> PIP3 Install Django

To add an environment variable:

Add these directories to the system environment variable:

C:/python33/lib/site-packages/django; C:/python33/scripts. Once added, you can use the Django django-admin.py command to create a new project.

Check to see if the installation was successful:

3. Create Project

>django-admin.py Startproject MySite

Note: Since previously created here using Mysite_django

 

This will automatically generate several directories:

In Django, the basic project contains four files:

Mysite_django

__init__.py: Tell Python that this is a package

settings.py: Project-related configuration files

urls.py: Global URL configuration (routing system)

Wsgi.py:WSGI, WSGI socket is used by default

Manage.py:django Program startup file

  

  4. Create an App

You can create multiple apps under one project

> Python manage.py startapp CMDB #创建名为cmdb的app

This will see the CMDB that has been created:

Auto-generated application file under CMDB:

__init__.py: Tell Python is a package

admin.py: Default Background management

apps.py: Configuration file for current app

models.py: Data-related (data model)

tests.py: Unit Test

Views: View functions, processing business requests.

  5. Writing code

So far the project has been created and the App,urls and views.py files currently have no real code and need to add code after that.

Custom page:

urls.py: Add URL path, default to Admin

URLs views

4. Start the Django program

> Python manage.py runserver 127.0.0.1:8000

can also be performed via Pycharm:

Prompted us to visit Http://127.0.0.1:8000/after launch, but after the interview error 404 page does not exist, because our custom page is index, plus index can:

This allows you to see the result of the response.

5. Using templates

Settings configuration

Render (Request, ' path ')

First create a template path templates and create an HTML file index.html

Index

Next we need to configure the path in the config file settings, otherwise we can't find the HTML file:

Locate the "TEMPLATES" field to add the path:

Note: Base_dir: This file is defined at the beginning:

After restarting, access the view results:

You can see the HTML file that has been referenced by the customization.

6. Static file Configuration

Create a static file directory statics, where jquery is introduced:

1 <! DOCTYPE html> 2         hello,word10     

To modify the configuration file path:

1 # #settings最下边配置路径2 3 Static_url = '/prefix/' #前缀4 staticfiles_dirs = (5     os.path.join (base_dir, ' statics '), #js路 Diameter 6)

To restart the test:

F12 Open Debug mode and right click on our own definition of jquery file, open, you can see the content description success

7. Connect to the database

Orm

settings.py

 

(1) Define database user information

1 #models. py Database 2 3 from django.db import models4 5 # Create Your models here.6 #创建类定义用户信息7 class UserInfo (models.m Odel): 8     user = models. Charfield (max_length=32) #指定字符串长度9     email = models. Charfield (max_length=32) 

(2) Register app

settings.py

1 #修改INSTALLED_APP字段, adding custom App:cmdb  2  3 Installed_apps = [4     ' Django.contrib.admin ', 5     ' Django.contrib.auth ', 6     ' Django.contrib.contenttypes ', 7     ' django.contrib.sessions ', 8     ' Django.contrib.messages ', 9 ' django.contrib.staticfiles ', ' CMDB '     

(3) Create a database with a SQLite database by default

settings.py

Execute command creation:

> Python manage.py makemigrations

> Python manage.py Migrate

Database tables have been created

8. Operational database

Create:

Models. Class. Objects.create ()

Get:

Models. Class. Objects.all ()

    

Python's web framework 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.