Tag Archives: django

Django Use logging Module Cannot Operate the File Error: logging error Permission Error [WinError 32]

Causes:

This problem is only encountered during development, and the configuration is written to setting Py configuration file. We define the log file size. When the log is full, we will encounter this problem, because when running Django with pychart, we enable both processes to initialize setting Py configuration, one is accessed normally, and the other is used to monitor code changes (you will find that Django will rebuild itself every time you modify some code, which is completed by this process). Because it occupies the handle of the file, it fails during archiving

Solution:

Method 1: add the — noreload parameter when starting Django

For example, python manage py runserver 127.0.0.1:8080 –noreload

After adding the — noreload parameter, when the development server is started, the Python code of Django project is modified, and the server will not restart automatically

Method 2:

Expand log files

Modify setting.Py to expand maxbytes

[Solved] python3.7+django2.2 Error: AttributeError: ‘str’ object has no attribute ‘decode’

Environment: python3.7+django2.2
Error message:
AttributeError: ‘str’ object has no attribute ‘decode’

Solution:
Find the django file under the python file>db file>backends>mysql>operations.py
open a file:
Ctrl+f search query.decode after opening

 

Then change query.decode to query.encode
#Original code:

query = getattr(cursor, '_executed', None)
if query is not None:
query = query.decode(errors='replace')
return query

#change into:

query = getattr(cursor, '_executed', None)
if query is not None:
query = query.encode(errors='replace')
return query

Done!

[Solved] Django Error: – no such table: main.auth_user__old

Django reports an error no such table: main auth_user__old

 

An error is reported when adding an article in the admin background

 

Django 2.0, I checked it. The reason for the error is that Django version is low and SQLite3 database is compatible

 

Solution:

Upgrade Django to 2.1.0 five

pip install django==2.1. 5 

Just enter the command directly, and version 2.0 will be uninstalled automatically.

 

 

Delete migrations folder

 

Delete SQLite database

 

 

Migrate the default app first

python manage.py migrate

Then migrate your own app myblos

python manage.py makemigrations myblogs
python manage.py migrate myblogs

 

 

Re create administrator account and password

Just go back in and add the article

[Solved] Django database Modify Warning: Did you rename house.houseid to house.id (a BigAutoField)? [y/N] n

1. Problem description

In Django, models Add an order table in the PY file. When migrating through the “py manage.py makemigrations” command, I find that a small black box appears, asking me to rename the ID of the existing table in the database, prompting that adding a non empty “Id” to an existing house table is not allowed; Specific tips are as follows:

2. Problem solving

The following solutions have been tried:

1. Comment out models The code of the new order table in py re executes “py manage.py makemigrations”, and it is found that the above error is still prompted.

2. Someone in stackoverflow has encountered a similar situation (the specific link is here), and the reply solution is as follows:

This might sound dumb but did you perhaps run a find and replace in your project where you replaced id with pid? Django automatically generates primary key fields with the name id not pid. This or perhaps previously you ran a modified version of Django which generated fields named pid instead of id is what I can think happened. As below comment states just edit your migration files and correct them.

The main idea is that this problem is quite outrageous. It is possible that the user-defined primary key in models is ID, but the PID is used in the code;

After checking the code, it is found that one interface with parameters uses “houseid” instead of models After replacing “houseid” with “Id” in the customized primary key “Id” in py, run “py manage.py makemigrations” and find no error.

 1 views.py
 2 
 3 def addToHouseOrder(request, houseid):
 4     """
 5     Users add houses to the order list
 6     :param request:
 7     :param id: houseid
 8     :return:
 9     """
10     if request.method == "GET":
11         currentUser = request.session['username']
12         print(currentUser, houseid)
13         houseResult = House.objects.filter(id=houseid)
14         return HttpResponse(houseid)
15     elif request.method == "POST":
16         pass

Causes and solutions of Django’s unique error reporting

 


1 RuntimeError at /login
2 You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8009/login/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

Django was a vengeance alone.

:

: <

:

:

How to Solve Django xadmin installation Error [7 Types of Errors]

Error 1: ModuleNotFoundError: No module named ‘django.core.urlresolvers’

1 ModuleNotFoundError: No module named 'django.core.urlresolvers'

Solution: find the wrong file according to the prompt, and change all import django.core.urlresolvers to   import django.urls

import django.core.urlresolvers 

#change to
import django.urls

Error 2: TypeError: __init__() missing 1 required positional argument: ‘on_delete’

TypeError: __init__() missing 1 required positional argument: 'on_delete'

Solution: This is basically an error in models. Foreignkey() in the models file. Add on in parentheses according to the Django document_delete=models.CASCADE

Error 3: typeerror:__init__() takes 1 positional argument but 6 were given

TypeError: __init__() takes 1 positional argument but 6 were given

Solution: hint that the file xadmin\views\dashboard.py, find

 

forms.Field.__init__(self, required, widget, label, initial, help_text,  *args, **kwargs)

change to

forms.Field.__init__(self)

 forms.Field.__init__(self, required, widget, label, initial, help_text,  *args, **kwargs)

#change to
forms.Field.__init__(self)

Error 4: ImportError: cannot import name ‘login’ from ‘django.contrib.auth.views’

ImportError: cannot import name 'login' from 'django.contrib.auth.views'

Solution: The hint is in xadmin\views\website.py”, line 5, in <module>
from django.contrib.auth.views import login

find the location, and then change

from django.contrib.auth.views import login
from django.contrib.auth.views import logout

to

from django.contrib.auth import authenticate, login, logout

from django.contrib.auth.views import login
from django.contrib.auth.views import logout

#change to
from django.contrib.auth import authenticate, login, logout

 

Error 5: ImportError: cannot import name ‘QUERY_TERMS’ from ‘django.db.models.sql.query’

 ImportError: cannot import name 'QUERY_TERMS' from 'django.db.models.sql.query'

Solution: find the location in xadmin\plugins\filters.py”, line 10, in <module>

then change

from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS

to

from django.db.models.sql.query import LOOKUP_SEP, Query

from django.db.models.sql.query import LOOKUP_SEP, QUERY_TERMS

#change to
from django.db.models.sql.query import LOOKUP_SEP, Query

Error 6: ImportError: cannot import name ‘password_reset_confirm’ from ‘django.contrib.auth.views’

ImportError: cannot import name 'password_reset_confirm' from 'django.contrib.auth.views'

Solution: in\xadmin\plugins\passwords.py”, line 4, in <module>
Find    from django.contrib.auth.views import password_reset_confirm

 

change to   from django.contrib.auth.views import PasswordResetConfirmView

in line 77, change   return password_reset_confirm

to   return PasswordResetConfirmView

from django.contrib.auth.views import password_reset_confirm
#change to  
from django.contrib.auth.views import PasswordResetConfirmView

#line 77 
return password_reset_confirm
#to  
return PasswordResetConfirmView

Error 7: AttributeError: ‘Settings’ object has no attribute ‘MIDDLEWARE_CLASSES’

AttributeError: 'Settings' object has no attribute 'MIDDLEWARE_CLASSES'

Solution:   in xadmin\plugins\language.py”, line 24, in <module>
if settings.LANGUAGES and ‘django.middleware.locale.LocaleMiddleware’ in settings.MIDDLEWARE_CLASSES:

change to if settings.LANGUAGES and ‘django.middleware.locale.LocaleMiddleware’ in settings.MIDDLEWARE:

 

if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE_CLASSES:
#change to
if settings.LANGUAGES and 'django.middleware.locale.LocaleMiddleware' in settings.MIDDLEWARE:

Other errors are modulenotfounderror: no module named, just install the corresponding module according to the prompt. If it is not installed, it may be that the module name is written incorrectly

Django startup error: generator expression must be parentized

Return to Django directory

Startup error: syntax error: generator expression must be parented . Sometimes, when using the command Python manager.py runserver , the startup fails first. The error log is as follows:

[[email protected] es_demo]# python37 manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f220f0edae8>
Traceback (most recent call last):
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/utils/autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/usr/local/python/python37/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in <module>
    from django.contrib.admin.filters import (
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in <module>
    from django.contrib.admin.options import IncorrectLookupParameters
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/contrib/admin/options.py", line 12, in <module>
    from django.contrib.admin import helpers, widgets
  File "/usr/local/python/python37/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 151
    '%s=%s' % (k, v) for k, v in params.items(),
    ^
SyntaxError: Generator expression must be parenthesized

It can be found that the last line indicates that there is a syntax error and that there is a problem with the generator expression. The specific error line content is '% s =% s'% (k, V) for K, V in params. Items(), . You can see that the last punctuation is very interesting. We can find the line corresponding to the source file and remove this punctuation

PS: if it is modified in pycharm, since this file is the source code, you should also select I want to edit this file anyway in the prompt box


welcome to Fuzheng, that’s all see also: [django1.11 startup error: generator expression must be parentized]( https://www.cnblogs.com/yanlin-10/p/9714793.html )

[Solved] Django use search_fields error (in get_lookup_constraint)

django use search_fields error (in get_lookup_constraint)

FieldError at /api/workorder/order/

Related Field got invalid lookup: icontains
Request Method: GET
Request URL: http://127.0.0.1:8000/api/workorder/order/?search=1
Django Version: 2.0
Exception Type: FieldError
Exception Value: Related Field got invalid lookup: icontains
Exception Location: C:\Users\WM\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\sql\query.py in build_lookup, line 1075
Python Executable: C:\Users\WM\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.5
Python Path: [‘F:\git_code\emergency_itil\EmergencyResponse’, ‘F:\git_code\emergency_itil\EmergencyResponse’, ‘E:\PyCharm 2019.1.1\helpers\pycharm_display’, ‘C:\Users\WM\AppData\Local\Programs\Python\Python36\python36.zip’, ‘C:\Users\WM\AppData\Local\Programs\Python\Python36\DLLs’, ‘C:\Users\WM\AppData\Local\Programs\Python\Python36\lib’, ‘C:\Users\WM\AppData\Local\Programs\Python\Python36’, ‘C:\Users\WM\AppData\Roaming\Python\Python36\site-packages’, ‘C:\Users\WM\AppData\Local\Programs\Python\Python36\lib\site-packages’, ‘E:\PyCharm 2019.1.1\helpers\pycharm_matplotlib_backend’]
Server time: Monday, 22 July 2019 21:07:21 +0800

 

Reason:

search_fields = ('content', 'detail','submitter')

This is a fuzzy query field in the view. The submitter field is a foreign key ForeignKey field. As a foreign key, it corresponds not to a specific field, but to a class

Therefore, we should map it to a specific field associated with a foreign key, such as submitter__username

search_fields = ('content', 'detail','submitter__username')

Error modulenotfounderror when starting Django: no module named ‘pytz’

It was a bad start. I made a mistake when I first learned Django. No module named ‘pytz’ is reported when running the manage.py file through the PY – 3 manage.py runserver 127.0.0.1:8000 command

Solution: install pytz module: PIP3 install pytz

After successful installation, enter py – 3 manage.py runserver 127.0.0.1:8000 to run successfully:

  If you want to directly run the manage.py file in pycharm, you need to add parameters

First open the edit configuration, as shown in the figure:

Add parameters as shown in the figure: