ValueError:源代码字符串在Django中不能包含空字节

时间:2020-04-26 06:30:29

标签: django django-models django-views django-templates

现在,我正在学习django框架,但遇到以下问题 这是错误消息

    Exception ignored in thread started by: <function check_errors.<locals>.wrapper at 0x044BF148>
Traceback (most recent call last):
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper   
    fn(*args, **kwargs)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\management\commands\runserver.py", line 
117, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\management\base.py", line 376, in check
    all_issues = self._run_checks(
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\utils\functional.py", line 37, in __get__    
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\utils\functional.py", line 37, in __get__    
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\web_projects\pages\pages_project\urls.py", line 21, in <module>
    path('', include('pages.urls')),
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\nodvo\.virtualenvs\web-e0x1-Fid\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 779, in exec_module
  File "<frozen importlib._bootstrap_external>", line 916, in get_code
  File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ValueError: source code string cannot contain null bytes

我创建了一个名为pages的应用,然后将该应用添加为settings.py,以pages.apps.PagesConfig的形式尝试将models.py文件的格式更改为 UTF-8 ,但是不适用于我。

这是pages.urls文件

from django.urls import path

from .views import HomePageView

urlpatterns = [
    path('', HomePageView.as_view(), name='home'), ]

这是views.py文件

from django.views.generic import TemplateView


class HomePageView(TemplateView):
    template_name = 'home.html'

这是另一个urls.py文件。

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')), 
]

0 个答案:

没有答案