我正在为基于Django的静态博客编写代码,但我在代码的3个或4个不同区域遇到了类似的问题。我想如果我可以修复一个,那么我也可以修复其他修复。我的焦点代码是django-command,我称之为update_blog1
。这是追溯......
Traceback (most recent call last):
File "C:\Python34\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/firstSite/PROJECTone\blog_static\views.py", line 179, in archive
{'posts' : posts}
File "C:/Users/Jaysp_000/firstSite/PROJECTone\blog_static\views.py", line 14, in render_response
return render_to_response(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\shortcuts.py", line 45, in render_to_response using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py", line 116, in render_to_string
template_name, context, context_instance, dirs, dictionary)
File "C:\Python34\lib\site-packages\django\template\engine.py", line 221, in render_to_string
return t.render(context_instance)
File "C:\Python34\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:\Python34\lib\site-packages\django\template\context.py", line 235, in bind_template
updates.update(processor(self.request))
File "C:\Python34\lib\site-packages\django\template\context_processors.py", line 56, in i18n
context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi()
File "C:\Python34\lib\site-packages\django\utils\translation\__init__.py", line 177, in get_language_bidi
return _trans.get_language_bidi()
File "C:\Python34\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'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\formats.py", line 103, in get_format
cached = _format_cache[cache_key]
KeyError: ('r', None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.3\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python34\lib\runpy.py", line 182, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "C:\Python34\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/Jaysp_000/firstSite/PROJECTone\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 441, in execute
output = self.handle(*args, **options)
File "C:/Users/Jaysp_000/firstSite/PROJECTone\blog_static\management\commands\update_blog1.py", line 78, in handle
resp = client.get(path)
File "C:\Python34\lib\site-packages\django\test\client.py", line 500, in get
**extra)
File "C:\Python34\lib\site-packages\django\test\client.py", line 303, in get
return self.generic('GET', path, secure=secure, **r)
File "C:\Python34\lib\site-packages\django\test\client.py", line 379, in generic
return self.request(**r)
File "C:\Python34\lib\site-packages\django\test\client.py", line 448, in request
response = self.handler(environ)
File "C:\Python34\lib\site-packages\django\test\client.py", line 122, in __call__
response = self.get_response(request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 261, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "C:\Python34\lib\site-packages\django\views\debug.py", line 97, in technical_500_response
html = reporter.get_traceback_html()
File "C:\Python34\lib\site-packages\django\views\debug.py", line 384, in get_traceback_html
return t.render(c)
File "C:\Python34\lib\site-packages\django\template\base.py", line 209, in render
return self._render(context)
File "C:\Python34\lib\site-packages\django\template\base.py", line 201, in _render
return self.nodelist.render(context)
File "C:\Python34\lib\site-packages\django\template\base.py", line 903, in render
bit = self.render_node(node, context)
File "C:\Python34\lib\site-packages\django\template\debug.py", line 79, in render_node
return node.render(context)
File "C:\Python34\lib\site-packages\django\template\debug.py", line 89, in render
output = self.filter_expression.resolve(context)
File "C:\Python34\lib\site-packages\django\template\base.py", line 674, in resolve
new_obj = func(obj, *arg_vals)
File "C:\Python34\lib\site-packages\django\template\defaultfilters.py", line 771, in date
return formats.date_format(value, arg)
File "C:\Python34\lib\site-packages\django\utils\formats.py", line 136, in date_format
return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
File "C:\Python34\lib\site-packages\django\utils\formats.py", line 110, in get_format
for module in get_format_modules(lang):
File "C:\Python34\lib\site-packages\django\utils\formats.py", line 82, in get_format_modules
modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH)))
File "C:\Python34\lib\site-packages\django\utils\formats.py", line 51, in iter_format_modules
if not check_for_language(lang):
File "C:\Python34\lib\site-packages\django\utils\translation\__init__.py", line 181, in check_for_language
return _trans.check_for_language(lang_code)
File "C:\Python34\lib\functools.py", line 472, in wrapper
result = user_function(*args, **kwds)
File "C:\Python34\lib\site-packages\django\utils\translation\trans_real.py", line 409, in check_for_language
if not language_code_re.search(lang_code):
TypeError: expected string or buffer
以下是 update_blog1
的代码from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse
from django.test.client import Client
import sys, os
from optparse import make_option
from P1config.settings import STATICBLOG_POST_DIRECTORY, STATICBLOG_COMPILE_DIRECTORY
class Command(BaseCommand):
help = "Compile blog posts from html to markdown, and upload images to S3 Defaults to processing only new blog posts"
option_list = BaseCommand.option_list + (
make_option(
'--all',
action='store_true',
dest='all',
default=False,
help='Get all blog posts, regardless of date'
),
make_option(
'--name',
action='store',
dest='post_name',
default=False,
help='Get named blog post'
),
)
def handle(self, *args, **options):
verbosity = int(options.get('verbosity'))
client = Client()
outdir = STATICBLOG_COMPILE_DIRECTORY
posts = []
previews = []
if options['all']:
if verbosity > 3:
print ('Compiling all blog posts')
posts = self._get_all_posts()
elif options['post_name']:
posts = self._get_named_posts(options['post_name'])
else:
if verbosity > 3:
print ('Compiling new blog posts')
posts = self._get_all_posts(new = True)
if verbosity > 3:
print ('%d posts found' % len(posts))
print ('----------------------------')
for post in posts:
if verbosity > 3:
print ("Compiling " + post['md_name'] + " to " + post['html_name'])
path = reverse('blog_static.views.archive') + post['path']
# path = '/preview/' + post['path']
resp = client.get(path)
if os.path.exists(outdir + post['path']) == False:
try:
with open(outdir + post['path'], 'r') as f:
pass
except IOError as e:
os.mkdir(outdir + post['path'])
with open(outdir + post['html_name'], 'wb') as f:
f.write(resp.content)
if len(posts) > 0 and verbosity > 3:
print ('----------------------------')
if verbosity > 3:
print ('Updating listings...')
print ('----------------------------')
path = reverse('blog_static.views.archive')
resp = client.get(path)
with open(STATICBLOG_COMPILE_DIRECTORY + 'index.html', 'wb') as f:
f.write(resp.content)
if verbosity > 3:
print ('Done')
def _get_all_posts(self, new = False):
posts = []
for item in os.listdir(STATICBLOG_POST_DIRECTORY):
post = self._create_post(item, new)
if post:
posts.append(post)
return posts
def _get_named_posts(self, post):
post_list = post.split(',')
posts = []
for item in post_list:
try:
with open(STATICBLOG_POST_DIRECTORY + item, 'r') as f:
post = self._create_post(item)
if post:
posts.append(post)
except IOError as e:
print ('\033[01;31m' + str(e) + '\033[0m', file= sys.stderr)
return posts
def _create_post(self, item, new = False):
outdir = STATICBLOG_POST_DIRECTORY
compiled_post = {
'md_name' : item,
'html_name' : '',
'path' : '',
'html' : '',
}
if item.endswith('.md'):
compiled_post['path'] = item.replace('.md', '')
compiled_post['html_name'] = compiled_post['path'] + '/index.html'
if new:
try:
with open(STATICBLOG_COMPILE_DIRECTORY + compiled_post['html_name'], 'r') as f:
return False
except IOError as e:
return compiled_post
else:
return compiled_post
如果查看代码,方法_get_named_posts()
,_create_post()
,handle()
每个都嵌入了open()函数。正如PyCharm所指出的那样,这些open()函数(例如open(outdir + post['path'], 'r'
或open(STATICBLOG_POST_DIRECTORY + item, 'r')
)就是问题所在。
在我view.py file
中,我得到了这个......
# Django imports
from django.template import RequestContext
from django.shortcuts import render_to_response, render
from django.core.files.storage import get_storage_class
from django.core.files.base import ContentFile
from django.views.decorators.csrf import csrf_exempt
from django.db import models
# Create a 'shortcut' function to wrap request in RequestContext()
def render_response(req, *args, **kwargs):
"""Shortcut to wrap request in RequestContext"""
kwargs['context_instance'] = RequestContext(req)
return render_to_response(*args, **kwargs)
# Standard Python lib
import os, sys, urllib, hashlib
# 3rd party apps
import markdown
from markdown.inlinepatterns import ImagePattern, IMAGE_LINK_RE
# from config folder
from P1config.settings import STATICBLOG_COMPILE_DIRECTORY, \
STATICBLOG_POST_DIRECTORY, \
STATICBLOG_STORAGE
###################################################################################
class S3ImagePattern(ImagePattern):
""" Wrapper class to handle image matches in markdown document """
def handleMatch(self, match):
node = ImagePattern.handleMatch(self, match)
# check 'src' to ensure it is local
src = node.attrib.get('src')
storage_class = get_storage_class(STATICBLOG_STORAGE)
storage = storage_class()
# otherwise we need to do some downloading!
if 'http://' in src or 'https://' in src:
img_data = urllib.request.urlopen(src).read()
md5 = hashlib.md5()
md5.update(img_data)
name = md5.hexdigest() + '/' + os.path.basename(src)
else:
with open(STATICBLOG_POST_DIRECTORY + src) as fhandle:
img_data = fhandle.read()
name = src
print('Uploading ' + src, file=sys.stderr)
try:
storage.save(name, ContentFile(img_data))
node.attrib['src'] = storage.url(name)
print ('Uploaded ' + src + ' to ' + storage.url(name), file=sys.stderr)
except Exception as e:
print(str(e), file=sys.stderr)
print ('\033[01;31mUpload of %s failed\033[0m' % src, file=sys.stderr)
return node
def render_post(request, post_name):
""" Render a blog post based on a .post template
The used template is rendered as html in the folder defined
by STATICBLOG_COMPILE_DIRECTORY
"""
content = ""
mdown = markdown.Markdown(extensions = ['meta',])
mdown.inlinePatterns['image_link'] = S3ImagePattern(IMAGE_LINK_RE, mdown)
try:
post_file_dir = os.path.join(STATICBLOG_POST_DIRECTORY, post_name + '.md')
with open(post_file_dir, 'r') as pfDIR:
content = pfDIR.read() # opening and reading the ENTIRE '.md' document
html = mdown.convert(content) # converting file from '.md' to ".html"
except IOError as e:
print (str(e))
with open(os.path.join(STATICBLOG_POST_DIRECTORY, 'preview2.md')) as f:
content = f.read()
html = mdown.convert(content)
post = { 'content' : html, }
try:
post['date'] = mdown.Meta['date'][0]
post['title'] = mdown.Meta['title'][0]
post['author'] = mdown.Meta['author'][0]
post['summary'] = mdown.Meta['summary'][0]
post['tags'] = mdown.Meta['tags'][0]
except:
pass
meta = {}
if 'title' in post:
meta['title'] = post['title']
# Context Object containing the post, meta contexes
context = {'post' : post, 'meta' : meta}
return render_response( # but could I just use render?
request,
'post2.html',
context
)
def archive(request):
mdown = markdown.Markdown(extensions = ['meta',])
# Create an empty post list for now
posts = []
import string
# Look at every 'item' in the STATICBLOG_COMPILE_DIRECTORY
for item in os.listdir(STATICBLOG_COMPILE_DIRECTORY):
# if the 'item' in this directory ends with '.post' (like '.md' in a markdown file, or '.py' in a python file)
# More specifically, if there is a '.post' file located in this directory...
if item.endswith('.md'):
# ...continue on and...
continue
# ...attempt to use that 'item'
try:
# ...by opening it,
with open(os.path.join(STATICBLOG_POST_DIRECTORY, item + '.md')) as fhandle:
# ...reading the markdown file,
content = fhandle.read() # (opening and reading the ENTIRE '.md' document)
# ...and converting it to HTML.
mdown.convert(content) # (converting file from '.md' to ".html")
post = { 'name' : item, }
if 'title' in mdown.Meta and len(mdown.Meta['title'][0]) > 0:
# Add the markdown document's 'title' to post[]
# This stores the post's title from the "Meta" section of the '.md' document
post['title'] = mdown.Meta['title'][0]
# but if that doesnt work...
else:
# Add to the post list the item's Meta 'title', which simply takes \n
# the title of the .md document and removes the '-' from it, so that \n
# we can make it the post's title.
post['title'] = string.capwords(item.replace('-', ' '))
# ...and if there exists a 'date' in the item's meta attribute...
if 'date' in mdown.Meta:
# pass the 'date' info found in the Meta attribute to a 'date' \n
# variable created in the post list
post['date'] = mdown.Meta['date'][0]
posts.append(post)
except:
pass
from operator import itemgetter
posts = sorted(posts, key=itemgetter('date'))
posts.reverse()
return render_response( # but could I just use render?
request,
'archive.html',
{'posts' : posts}
)
@csrf_exempt
def handle_hook(request):
from django.http import HttpResponse
from django.core.management import call_command
result = call_command('update_blog', verbosity = 0)
return HttpResponse(result)
我不知道如何解决这个问题。你能告诉我如何解决这些问题吗?我不知道我可以在我的代码中放置字符串或缓冲区。