Some mistakes and solutions in Django

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: