Tag Archives: Django Error

How to Solve Django Error: Uncaught SyntaxError:Unexpected token<

I recently built a django + vue project platform. When I was a CV engineer last night, I encountered a bug and the information returned on the page was wrong.

 

The error message on the web page is as follows: Uncaught SyntaxError: Unexpected token< 

 

 

Solution: In the data layer, the operation method of a database is wrong. The original method only needs to be displayed. Currently, it needs to be combined with VUE to realize the real-time display function of the search box. The original variable cannot be traversed, so an error is reported.

 

Code like this

Original code: hrefs = DB_hrefs.obiject.all()

After modification: hrefs = list(DB_hrefs.object.all().values())

 

The problem can be solved after the modification is completed.

How to Solve Django Error: No module named ‘MySQLdb’

Since many related dependent packages were unloaded when MySQL was unloaded, the following error occurred when Django was started after reloading MySQL:

django.core.exceptions.ImproperlyConfigured:Error loading MySQLdb module: No module named 'MySQLdb".
Did you install mysqlclient or MySQL-python?

Due to the development of Python version 3.6.4, mysql-python does not support python3, after a lot of trouble to install mysqlclient

The following is my process to solve the problem, I am lazy, I will show you in the form of pictures

First of all, I tried to install MySQL python, but there was an error. The following is the solution for online search:

Here’s how I found the solution and installed mysqlclient:

The following is the code to install mysqlclient and its dependent environment:

pip install mysqlclient
sudo apt-get install python3-dev libmysqlclient-dev

Conclusion:

When there is a problem with no module named ‘MySQL db’. It is recommended to install mysqlclient. MySQL Python does not support python3

How to Solve Django Error: No such column: xxx

The operation before and after the model is as follows:

First migration:

class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=True)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)

After change:

class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=True)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
    owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
    highlighted = models.TextField()

There are three steps for Django to modify the database

1. Make changes to models.py (normal)

2. Creating migrations using makemigrations

3. Migration using migrate

Analysis: after the modified model, the code and highlighted fields cannot be empty when the database is synchronized for the second time. If you create the highlighted field the first time, there is no impact because the table does not have an existing row. If the data table has been created for the first time and data has been inserted into the table, a default value must be defined to fill in the existing row, otherwise the database will not accept the data table changes because of violating the integrity of the data

Solution: change to

code = models.TextField(default =”)

highlighted = models.TextField(default =”)