Summary of django's experiences over the past few days

Source: Internet
Author: User
Tags postgresql syntax psql django website django tutorial

Python is a very simple and easy-to-use programming language. I was familiar with Java, but after using Python, I found that the python syntax has a very clear structure, such as forced indentation and empty lines, it makes people feel that the written code is neat and beautiful. In addition, there are a lot of Python frameworks, from webpy, jdango. It is very fast to use python for development. Jdango is heavy in terms of performance. It can support much better than other frameworks. In addition, there are many open-source python libraries. We can use pip sudo pip install XXX in python to facilitate installation. Khan, I have been using python setup before. py install. download them one by one .....

Speaking of python. If you are not familiar with the python syntax, you can directly read basic python tutorials or other articles on the Internet. Some students like to buy books and I have bought a few books, but I have never read them, everything is learned from the Internet. Although it is a bit complicated, it is good to sum up frequently.

Now, let's start learning about Django. First, we recommend that you use pythontip.com to learn about Django and python. After you have learned these things, it is estimated that python and jdango have all entered the door.

Ps I used PyCharm on mac to develop python.

1. How to Create a Django Project

A project contains many Apps

First you enter a folder (you have installed jdango, you can use setup. py or pip as I mentioned earlier) and then execute the django-admin.py startproject mysite so that a mysite will be created under the Directory

    : Let Python treat this directory as a file required by an Development Kit (that is, a group of modules. This is an empty file. You do not need to modify it.

    : A command line tool that allows you to interact with this Django project in multiple ways. Type To see what it can do. You should not need to edit this file; generate it in this directory for convenience.

    : Set or configure the Django project. View and understand the available setting types and their default values in this file.

    : Configure the Django project URL. It can be viewed as the directory of your django website. Currently, it is empty.




Change End Interface

The command starts the development server on port 8000 and only listens to local connections. To change the server port, you can pass the port as a command line parameter:



1. Version x, http://www.djangoproject.com/r/python-pgsql/1 /.

PostgreSQL2. Version x, http://www.djangoproject.com/r/python-pgsql /.

MySQL, Http://www.djangoproject.com/r/python-mysql.

SQLite requires no adapter if Python 2.5 + is used.

, Http://www.djangoproject.com/r/python-sqlite /.

Oracle, Http://www.djangoproject.com/r/python-oracle.




CreatesDirectory. Let's take a look at the contents of this directory:



Enter the following content:

. Its parent class Model contains all the necessary methods for interacting with the database, and provides a concise and beautiful syntax for defining database fields. Believe it or not, these are all the code we need to write to access basic data through Django.

) Is equivalent to the field type of the database (for example). For example,The module is equivalent to the following table (using PostgreSQL'sSyntax description ):

Statement.

There isCalled. This field indicates that one or more authors of a book,The database table does notField. On the contrary, Django creates an additional table (Multiple-to-multiple join tables) to process the ing between books and authors.

ActivateThese models. SetAdd the app to the list of installed apps in the configuration file to complete this step.

File, find.Tells Django projects which apps are activated. The default value is as follows:

EveryThe tuple element is followed by a comma, whether it has only one element or not. This is to avoid forgetting to add commas, and there is no harm .)

Instruct us to writeApp.Each app in uses the Python path description. The package path is separated by the decimal point.

Command to check whether the syntax and logic of your model are correct. If everything is normal, you will seeMessage. If an error occurs, check the model code you entered. The error output provides useful error information to help you correct your model.

. It helps you capture common model definition errors.

Statement (if you are using Unix, you can enable syntax highlighting ):

Is the name of the app. Run with you. After execution, the output is as follows:

    ) And lower-case model name (,,. You can refer to appendix B to rewrite this rule.

    Primary key. You can reset it.

    Suffix to foreign key field name. You guessed it. This is also customizable.

    The statement is clearly defined.

    The statement will be adjusted based on your database, such as some database-specific fields such as: (MySQL ),(PostgreSQL ),(SQLite) will be automatically generated.Similarly, the field name is automatically processed (for example, the single quotation marks are double quotation marks ). The output in this example is based on the PostgreSQL syntax.

The command does not actually create a data table in the database, but prints the SQL statement segment so that you can see what Django will do. If you want to do this, you can copy the SQL statements to your database client for execution, or directly perform operations through the Unix pipeline (for example, ''python manager. py sqlall books | psql mydb ''). However, Django provides a simpler way to submit SQL statements to the database: ''syncdb' command

Commands are a simple method to synchronize your model to the database. It willTo check the database. If the table does not exist, it will create it. Note that,AndNoSynchronize the modification or deletion of a model to the database. If you modify or delete a model and want to submit it to the database,And will not make any processing. (For more information, see the "Modify database architecture" section at the end of this chapter .)

, Nothing happens, because you have not added a new model or added a new app. ThereforeIt is always safe because it does not repeatedly Execute SQL statements.








Note: For more information, see the pythontip.com Django tutorial. Thank you.


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.