Python path _day107_django URL reverse resolution and database connection

Source: Internet
Author: User
Tags sqlite database

First, URL reverse resolution

Before we formally introduce the reverse parsing, let's start by introducing the URL parameter in Django, which is our URL instance, where the name parameter is its alias, which is the argument that we'll rely on in the inverse parsing that we're going to explain next.

1. Reverse parsing in template

On our front-end pages, such as form forms, we usually specify the path of the commit, but we used to write the path to death, which is obviously not very reasonable, because when the path in the URL needs to change, we need to change the page one by one. This is unreasonable. So we're going to use the alias of the URL to parse it backwards.

syntax: {'URL's alias ' %}

Examples are as follows:

2. Reverse resolution in view

In our view, such as redirect, we will also need to use the specified URL situation, the path to write dead is obviously not very reasonable, we can also be reversed to parse the way to obtain the corresponding path, the syntax is as follows:

Syntax:  from Import reversereverse ('URL alias ')

Examples are as follows:

Second, the URL name space

If, we have two app, through the route distribution, in two app's URLs file, if the URL alias of two app is the same, then the result of reverse parsing can be confusing, it will find the URL path of the URL alias in another app, which is obviously not what we want, The reason for this is that Django finds the URL alias in the global lookup. The workaround is to specify the namespace parameter namespace when routing the distribution as follows:

Once set up, the app does not have a problem when it is parsed in the following way:

Third, Django Connection database

We know that there are data migrations in Django that we have a default SQLite database for us to use, which is a Django-brought database that does not actually meet our actual production use, because it is configured in the Django setting file as follows:

DATABASES = {    'default': {        'ENGINE'  'django.db.backends.sqlite3',        'NAME  'db.sqlite3'),    }}

If we do not want to use the default database, then we can re-configure this parameter in the setting file, set to the database we want to connect, the following is the configuration of MySQL database, note that Django cannot be implemented to establish a database, So the database needs to be built on our own in CMD.

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'day107',#name of your database        'USER':'Root',#your database user name        'PASSWORD':"',#Your Database Password        'HOST':"',#your database host, leave blank default to localhost        'PORT':'3306',#your database port    }}

In addition to this, after configuring the above, also can not complete the migration of the database, the main reasons and solutions are as follows. If the interface code to replace the database with the replacement needs to be placed in the Django project or the application's int.py file, the automatic loading will be done when the project starts.

Python path _day107_django URL reverse resolution and database connection

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.