Some of Django’s common mistakes and fixes
The CSS and JS files fail to model the existing tables the data table field names don’t match the model field URL match problem view function passes the data problem
CSS and JS files are invalid
When you load a view with CSS and JS files, the easiest way to do this is to say href= “absolute path to file”, but a lot of times you write relative paths because it’s easier. If relative paths are used, static files cannot be found when a page submits a form or redirects to a view function with arguments. So load static file {% load static %} and use reverse parsing to make the page look for static files.
Model existing tables
To create ORM on tables that have already been in the database, run Python manager.py InspectDB & GT; Model.py to generate the model for the existing table, make sure to comment db_manage =False or delete it directly, otherwise Django has no permission to operate on the table.
The data table field name does not match the model field
The corresponding column is not a field name. You can implement the ORM by setting db_column = “corresponding field name” in the model
Url matching problem
When your two url matching rules are written very closely, if load is written on the top without any initial restriction (^), the following url will match the first one, even if it resolves
, such as the following two urls:
url(r'load/(?P<index>.*)/', views.load, name='load'),
url(r'manage_load/(?P<index>.*)/', views.manage_load, name='manage_load'),
Since the LOAD URL is written above, when you parse the URL backwards, {% URL “load” %}, by default Django matches urls from top to bottom, matching the first one because there is no restriction on the beginning and end.
url(r'^load/(?P<index>.*)/', views.load, name='load'),
url(r'manage_load/(?P<index>.*)/', views.manage_load, name='manage_load'),
You can use ^,$as a starting and ending character limit and it won’t match any other URL.
View functions pass data problems
request.session["index"] = index
request.session.flush()
In many cases, passing values from view function to view function is the simplest way to record data except passing parameters to view function. The following sentence can be used to clear session in the login interface or some fixed page, otherwise the data will exist until it is destroyed automatically by Django.
Read More:
- Common mistakes and solutions of cmake + QT + VTK
- Solutions to Django is not importable in this environment
- Django. DB. Utils. Programmingerror: (1146, “table * doesn’t exist”) causes and Solutions
- How to solve the problem of error reporting in the introduction of Python Django no module name ‘Django. Utils. Six’
- django.db.utils.OperationalError: no such table: django_admin_log
- Some mistakes in using NPM
- Mistakes in learning Flink programming
- Summary of some mistakes in the development of unity ar
- django.core.exceptions . improveconfigured: error loading MySQL DB module. Django configuring MySQL database
- Common mistakes in WCF distributed development (15): Communication with the underlying transaction manager has failed.
- When VS code imported Cv2, an error ImportError: numpy.core.multiarray failed to import; There are no mistakes in using the Spyder
- Running Django (2.2) project in Python 3.7.9 reports an error
- One of the common mistakes of tex
- Django reads the txt file in view
- Importerror of Django error: no module named**
- Four ways to get Django parameters in request
- Django + jQuery get data in the form + Ajax send data
- Problems and solutions of error reporting in using swiper in Vue
- When Python Django creates a database, can’t open file ‘ manage.py ‘: [Errno 2] No such file or directory
- Solutions to error 25090 in office 2003