Python3 Web framework: Django Basics

Source: Internet
Author: User
Tags install django

Ango Advantages and disadvantages Django is praised for its main areas:
    • Perfect documentation, Django's success, I think a big part of this is due to Django's near-perfect official documentation (including Django book).
    • A complete set of solutions, Django, like rails, offers a full suite of solutions (Full-stack Framework + batteries included), basically what's needed (e.g. cache, session, feed, ORM, GEO , auth), and all Django himself made, the development of Web site should also launch tools Django basically give you a good, so the development efficiency is needless to say, out of the problem is also easy to find, not in your code in the Django source.
    • With a powerful URL routing configuration, Django allows you to design a very elegant URL, and in Django you can basically say goodbye to ugly get parameters.
    • Self-service admin, admin interface is a more eye-catching contrib in Django, allowing you to have a complete background management interface almost without writing a single line of code.
Deficiencies:

The main drawback of Django is that Django insists on making all the wheels, the system is relatively closed, and Django's most reviled place is:

    • The system is tightly coupled, if you feel that the Django built-in feature is not very good, want to use a favorite third-party library to replace is difficult, such as the following will be said Orm, Template. It's almost impossible to use SQLAlchemy or Mako in Django, and even if you make some patches, you'll feel very, very awkward.

    • Django's own ORM is far less powerful than SQLAlchemy, except in the Django Acre, SQLAlchemy is the de facto ORM standard in the Python world, and other frameworks support SQLAlchemy, But Django still insists on its own set. The Django Developer's support for SQLAlchemy was also discussed and tried, but eventually it was abandoned, and it was estimated to be too expensive and hard to fit with other Django modules.

Django mode and its components

The nature of Django's MTV model is that in order to maintain a loosely coupled relationship between components, Django's MTV represents:

Model: Object that is responsible for business objects and databases (ORM)

Template (Template): Responsible for how to display the page to the user

View: Responsible for business logic, and call model and template when appropriate

In addition, Django has a URL dispatcher that distributes page requests for URLs to different view processes, and then calls the corresponding model and template

Django CentOS7 Installation
PIP3 Install Django

Django command

1. Create a Django Project
django-admin.py Startproject MySite

The current directory will generate a MySite project with the following directory structure:

    • manage.py-----A tool inside a Django project that can invoke Django shells and databases.
    • The settings.py----contains the default settings for the project, including database information, debug flags, and other work variables.
    • The urls.py-----is responsible for mapping the URL pattern to the application.
2, create the application in the MySite directory, such as blog:

Python manage.py Startapp Blog

Notice there is a hole here, when I execute the above command, the following error occurred:

Go into the source code and see.

The module is then imported into the command line and an error is found.

A search on the network because of a lack of dependencies when compiling and installing Python3 sqlite3 . The Pit!!!

Reinstall the dependent library, compile it

Yum Install sqlite-develmake cleanmake && make  Install

Re-creation succeeded:

3. Start the Django Project
Python manage.py runserver 8080

So our Django is up and up! When we visit: http://127.0.0.1:8080/, we can see:

4. Update database tables or fields synchronously

The initialization of the database, which is to create a library and a table for the project

    Python manage.py makemigrations    python manage.py migrate

This method creates a table, and when you add a class to the models.py, you can run it to automatically create a table in the database without creating it manually.

5. Clear the database
Python manage.py Flush

This command will completely empty the table of data under the project, leaving only an empty table

6. Create Super Administrator
    Python manage.py createsuperuser         # Follow the prompts to enter the user name and the corresponding password is OK The mailbox can be left blank, user name and password required         # Modify user password can be used:    python manage.py ChangePassword username

7. Django Project Environment Terminal
Python manage.py Shell

The difference between this command and running Python directly into the shell is that you can invoke the API in the models.py of the current project in this shell, which is very handy for testing the data.

The other One

Ython manage.py Dbshell

Django automatically enters the database set up in settings.py and, if it is MySQL or PostgreSQL, requires a database user password.

The SQL statement of the database can be executed at this terminal. If you're familiar with SQL, you might like this way.

More commands to help

Python manage.py

To see all the commands,忘记子名称的时候特别有用。

Python3 Web framework: Django Basics

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.