2016-01-03 22 views
6

मैं Django (क्योंकि इसका संसाधनपूर्ण तरीके से) का उपयोग करके एक स्थिर साइट जेनरेटर बनाने की कोशिश कर रहा हूं, और अभी मेरी समस्याएं Django कमांड से निपट रही हैं एक निर्देशिका में मेरी स्थिर साइट सामग्री बनाने के लिए माना जाता है। स्पष्ट रूप से मेरी 'noneType' ऑब्जेक्ट में कोई विशेषता 'विभाजन' नहीं है, लेकिन मुझे नहीं पता कि 'noneType' ऑब्जेक्ट क्या है।Django: AttributeError: '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' 

ऐसा लगता है कि मेरी समस्या मेरी आदेश फ़ाइल है, जो मैं build फोन में निहित है। ट्रेसबैक भी मेरी views फ़ाइल लाता है, जो अपने आप पर अच्छी तरह से काम करता है (यानी, मेरी एचटीएमएल फाइलों को स्थानीय सर्वर पर ठीक तरह से परोसा जा सकता है), लेकिन मैं इसे वैसे भी शामिल करूंगा।

:

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

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 

और सिर्फ मामले में यह पता करने के लिए उपयोगी हो जाता है, यहाँ मेरी 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'), 
] 

मुझे यह निराशाजनक लगता है, मुख्य रूप से क्योंकि यह समस्या रिवर्स() फ़ंक्शन से बंधी हुई है, जैसा कि बिल्ड मॉड्यूल में देखा गया है, और जब तक मुझे याद है, तब तक उस फ़ंक्शन का उपयोग करके मुझे बहुत अच्छा समय लगता था, लेकिन मुझे नहीं पता अगर यह वास्तव में मेरी समस्या है। क्या कोई मेरी मदद कर सकता है कि मेरी समस्या कहां से आ रही है और इसे कैसे हल किया जाए (यदि आपके पास कोई सुझाव है)? यह बहुत सराहनीय होगा।

+0

आपकी साइट का समर्थन i18n है? – doniyor

उत्तर

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

इस लाइन Django 1.8 में एक बग है ।

Prevented TypeError in translation functions check_for_language() and get_language_bidi() when translations are deactivated (#24569).

आप नवीनतम 1.8.x रिहाई, 1.8.8 नवीनीकृत करना चाहिए: यह 1.8.1 में ठीक किया गया था। लिखने के समय। यह इस बग और दूसरों को ठीक करेगा।

माइनर रिलीज़ में केवल बगफिक्स और सुरक्षा पैच होते हैं, इसलिए आपको हमेशा जो भी प्रमुख संस्करण उपयोग कर रहे हैं, उसके लिए आपको नवीनतम मामूली रिलीज में अपग्रेड करना चाहिए।

+0

स्वीकार्य के रूप में knbk के उत्तर की जांच करें प्रतिक्रिया के लिए धन्यवाद! मैं कोशिश करूँगा। क्या आप जानते हैं कि मुझे यह कैसे करना चाहिए? क्या मुझे डीजेंगो 1.8 को अनइंस्टॉल करना चाहिए और django 1.8.8 इंस्टॉल करना चाहिए या क्या मैं किसी भी तरह अपग्रेड कर सकता हूं? – JellisHeRo

+3

आप 'पाइप इंस्टॉल-यू Django <1.9' कर सकते हैं। – knbk

+0

धन्यवाद! आप और डोनियोर दोनों आपके समाधान में सही थे। उन्होंने वास्तव में मेरी मदद की! – JellisHeRo

5

अपने पृष्ठ दृश्य में एक भाषा सक्रिय करने का प्रयास:

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) 

ऐसा इसलिए है क्योंकि संदर्भ प्रोसेसर और संदर्भ भाषा पाने के लिए कोशिश कर रहा है यह स्पष्ट रूप से कोई नहीं है।

अद्यतन:

ठीक है, इस 1.8 में एक बग के रूप में knbk कहा है, तो आप नए संस्करण के लिए यह नवीनीकृत करने की आवश्यकता है ..

+0

आप दोनों सही थे! आपका जवाब मानता है कि मैं 1.8 में रहना चाहता था। धन्यवाद! – JellisHeRo

+1

@JellisHeRo धन्यवाद, कृपया – doniyor

संबंधित मुद्दे