简体   繁体   English

Django:AttributeError:'NoneType'对象没有属性'split'

[英]Django: AttributeError: 'NoneType' object has no attribute 'split'

I'm trying to build a static site generator using Django (because its resourceful that way), and right now my problems are dealing with the Django command that is supposed to build my static site content into a directory. 我正在尝试使用Django构建一个静态站点生成器(因为它的资源丰富),而现在我的问题是处理Django命令,该命令应该将我的静态站点内容构建到目录中。 Apparently my 'NoneType' object has no attribute 'split', but I dont know what that 'NoneType' object is. 显然我的'NoneType'对象没有属性'split',但我不知道'NoneType'对象是什么。

(thisSite) C:\Users\Jaysp_000\thisSite\PROJECTx>python prototype.py build
Traceback (most recent call last):
  File "prototype.py", line 31, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\core\management\__init__.py",
 line 338, in execute_from_command_line
    utility.execute()
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\core\management\__init__.py",
 line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\core\management\base.py", lin
e 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\core\management\base.py", lin
e 441, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Jaysp_000\thisSite\PROJECTx\sitebuilder\management\commands\build.py", li
ne 38, in handle
    response = this_client_will.get(the_page_url)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\test\client.py", line 500, in
 get
    **extra)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\test\client.py", line 303, in
 get
    return self.generic('GET', path, secure=secure, **r)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\test\client.py", line 379, in
 generic
    return self.request(**r)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\test\client.py", line 466, in
 request
    six.reraise(*exc_info)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\utils\six.py", line 659, in r
eraise
    raise value
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\core\handlers\base.py", line
132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Jaysp_000\thisSite\PROJECTx\sitebuilder\views.py", line 35, in page
    return render(request, 'page.html', context)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\shortcuts.py", line 67, in re
nder
    template_name, context, request=request, using=using)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\template\loader.py", line 99,
 in render_to_string
    return template.render(context, request)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\template\backends\django.py",
 line 74, in render
    return self.template.render(context)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\template\base.py", line 208,
in render
    with context.bind_template(self):
  File "C:\Python34\Lib\contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\template\context.py", line 23
5, in bind_template
    updates.update(processor(self.request))
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\template\context_processors.p
y", line 56, in i18n
    context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\utils\translation\__init__.py
", line 177, in get_language_bidi
    return _trans.get_language_bidi()
  File "C:\Users\Jaysp_000\thisSite\lib\site-packages\django\utils\translation\trans_real.
py", line 263, in get_language_bidi
    base_lang = get_language().split('-')[0]
AttributeError: 'NoneType' object has no attribute 'split'

It seems that my problem lies in my command file, which I call build . 看来我的问题出在我的命令文件中,我称之为build The traceback also brings up my views file, which works well on its own (that is, my html files can be properly served on the local server), but I will include it anyway. 回溯还会调出我的views文件,该文件本身运行良好(也就是说,我的html文件可以在本地服务器上正确提供),但无论如何我都会包含它。

build.py build.py

import os, shutil
from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse
from django.test.client import Client

def get_pages():
    for name in os.listdir(settings.STATIC_PAGES_DIRECTORY):
        if name.endswith('.html'):
            yield name[:-5]


class Command(BaseCommand):
    help = 'Build static site output.'

    def handle(self, *args, **options):
        """Request pages and build output."""
        if os.path.exists(settings.SITE_OUTPUT_DIRECTORY):
            shutil.rmtree(settings.SITE_OUTPUT_DIRECTORY)
        os.mkdir(settings.SITE_OUTPUT_DIRECTORY) 
        os.makedirs(settings.STATIC_ROOT)   
        call_command('collectstatic', interactive=False, clear=True, verbosity=0)
        this_client_will = Client()

        for page in get_pages():
            the_page_url = reverse('page',kwargs={'slug': page})      # PROBLEM SEEMS TO GENERATE STARTING HERE
            response = this_client_will.get(the_page_url)
            if page == 'index.html':
                output_dir = settings.SITE_OUTPUT_DIRECTORY
            else:
                output_dir = os.path.join(settings.SITE_OUTPUT_DIRECTORY, page)
                os.makedirs(output_dir)
            with open(os.path.join(output_dir, 'index.html'), 'wb', encoding='utf8') as f:
                f.write(response.content)

views.py views.py

import os
from django.conf import settings
from django.http import Http404
from django.shortcuts import render
from django.template import Template
from django.utils._os import safe_join

# Create your views here.

def get_page_or_404(name):
    """Returns page content as a Django template or raise 404 error"""
    try:
        file_path = safe_join(settings.STATIC_PAGES_DIRECTORY, name)
    except ValueError:
        raise Http404("Page Not Found")
    else:
        if not os.path.exists(file_path):
            raise Http404("Page Not Found")

    with open(file_path,"r", encoding='utf8') as f:
        the_page = Template(f.read())

    return the_page

def page(request, slug='index'):
    """ Render the requested page if found """
    file_name = '{0}.html'.format(slug)
    page = get_page_or_404(file_name)
    context = {'slug': slug, 'page': page} 
    return render(request, 'page.html', context)   # THE TRACEBACK POINTS AT THIS LINE, TOO

and just in case it becomes useful to know, here is my urls.py : 以防万一有用知道,这是我的urls.py

from django.conf.urls import include, url

urlpatterns = [
    url(r'^page/(?P<slug>[-\w]+)/$', 'sitebuilder.views.page', name='page'),
    url(r'^page$', 'sitebuilder.views.page', name='homepage'),
]

I find this frustrating, primarily because this problem seems to be tied to the reverse() function, as seen in the build module, and I havent had a great time using that function for as long as I can remember, but I dont know if this is really my problem. 我发现这令人沮丧,主要是因为这个问题似乎与reverse()函数有关,正如构建模块中所见,并且我没有很长时间使用该函数,只要我记得,但我不知道是否这真的是我的问题。 Can someone help me figure out where my issue is coming from and how to solve it (if you have any tips)? 有人可以帮我弄清楚我的问题来自何处以及如何解决(如果你有任何提示)? It would be much appreciated. 非常感谢。

base_lang = get_language().split('-')[0]

This line is a bug in Django 1.8. 这行是Django 1.8中的一个错误。 It was fixed as part of 1.8.1 : 它是作为1.8.1的一部分修复的:

Prevented TypeError in translation functions check_for_language() and get_language_bidi() when translations are deactivated ( #24569 ). 当翻译被停用时,翻译函数check_for_language()和get_language_bidi()中的Prevented TypeError( #24569 )。

You should upgrade to the latest 1.8.x release, 1.8.8. 您应该升级到最新的1.8.x版本1.8.8。 at the time of writing. 在撰写本文时。 That will fix this bug and others. 这将解决这个bug和其他问题。

Minor releases only contain bugfixes and security patches, so you should always upgrade to the latest minor release for whatever major version you're using. 次要版本仅包含错误修正和安全补丁,因此您应该始终升级到您正在使用的任何主要版本的最新次要版本。

try to activate a language in your page view: 尝试激活页面视图中的语言:

from django.utils import translation

def page(request, slug='index'):
    """ Render the requested page if found """
    file_name = '{0}.html'.format(slug)
    page = get_page_or_404(file_name)
    context = {'slug': slug, 'page': page} 
    translation.activate('en') # <------- Activate language EN
    return render(request, 'page.html', context) 

it is because context processor is trying to get the context language and it is obviously None. 这是因为上下文处理器试图获取上下文语言,它显然是无。

update : 更新

ok, this is a bug in 1.8 as knbk said, so you need to upgrade it to new version.. 好吧,这是1.8中的一个bug,因为knbk说,所以你需要将它升级到新版本..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django(Python)AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - Django(Python) AttributeError: 'NoneType' object has no attribute 'split' AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - AttributeError: 'NoneType' object has no attribute 'split' AttributeError: 'NoneType' object has no attribute 'split' 错误 - AttributeError: 'NoneType' object has no attribute 'split' Error lazyppredict - AttributeError: 'NoneType' 对象没有属性 'split' - lazyppredict - AttributeError: 'NoneType' object has no attribute 'split' AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39;? - AttributeError: 'NoneType' object has no attribute 'split'? Flask AttributeError:“ NoneType”对象没有属性“ split” - Flask AttributeError: 'NoneType' object has no attribute 'split' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - Python:AttributeError: 'NoneType' object has no attribute 'split' AttributeError: &#39;NoneType&#39; 对象在 pycharm 中没有属性 &#39;split&#39; - AttributeError: 'NoneType' object has no attribute 'split' in pycharm Django - AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;pk&#39; - Django - AttributeError: 'NoneType' object has no attribute 'pk' Django - AttributeError:“NoneType”对象没有属性“方法” - Django - AttributeError: 'NoneType' object has no attribute 'method'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM