2018.7.7-Problems with database configuration and home page

Source: Internet
Author: User

Database-related issues

The first problem encountered today is the database configuration problem, first of all the MySQL database to paste the common configuration:

1DATABASES = {2     'default': {3     'ENGINE':'Django.db.backends.mysql',4     'NAME':'dbname',5     'USER':'Root',6     'PASSWORD':'XXX',7     'HOST':"',8     'PORT':"',9     }Ten}
setting File Configuration summary
1 # since the Django internal connection to MySQL is using the MySQLdb module, and there is no such module in the Python3, it is necessary to use Pymysql instead 2   3 # The following settings are placed in the CONFIG. __init__.py file with the same name as Project 4   5 Import Pymysql 6 pymysql.install_as_mysqldb ()
_init_ File Configuration summary

Based on this configuration, the MySQL database can be used to complete the work behind it.

However, when I created the database, the system reported the following error

The code for the main error is as follows:

1 classTag (models. Model):2Nid = Models. Autofield (primary_key=True)3title = models. Charfield (verbose_name='Label name', max_length=32)4Blog = models. ForeignKey (verbose_name='Affiliated Blog', to='Blog', to_field='nid')#Error Line

The main reasons for this error are:

After django2.0, define the foreign key and a one-to-one relationship when you need to add on_delete option, this parameter in order to avoid the two table data inconsistency problem, otherwise will be error:

__init__ ' On_delete '

To illustrate:

User=models. Onetoonefield (User) owner=models. ForeignKey (UserProfile)

Need to change to:

User=models. Onetoonefield (user,on_delete=models. CASCADE) owner

Parameter description:
On_delete has cascade, PROTECT, Set_null, Set_default, SET () five selectable values
CASCADE: This value is set to cascade Delete.
PROTECT: This value is set to report integrity errors.
Set_null: This value is set to set the foreign key to NULL, provided that NULL is allowed.
Set_default: This value is set to the default value of the foreign key.
Set (): This value setting, which invokes the outside value, can be a function.
In general, the use of cascade is possible.

Problem with URL routing

When using path to configure URL routing, encountered the problem of regular expression can not be used, check the original Django 2.0 is a new change, if you need to use regular expressions can introduce

From Django.urls import Re_path

Specifically, you can refer to the Great God's blog.

For more information about the changes, you can refer to the official documentation.

2018.7.7-Problems with database configuration and home page

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.