def get_page_or_404(name):
    """Return page content as a Django template or raise 404 error.

    Given a page name, try get the absolute file path. If ValueError exception or in case
    file path doesn't exist raise a 404 error. If exists open each file and instantiates a new Django template object
    with its contents. Loop through the page's raw nodelist and checks for BlockNode with the name context. If BlockNode
    is found, it defines a metavariable for us that contains that content.

    :param name: the page name
    :raise ValueError: raises an exception
    :return: render as a template the page content inside page folder
    """
    try:
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)  # absolute file path
    except ValueError:  # exceptional outcome
        raise Http404('Page Not Found')
    else:  # not exceptional outcome
        if not os.path.exists(file_path):
            raise Http404('Page Not found')

    with open(file_path, 'r') as f:
        page = Template(f.read())  # instatiates a new <class 'django.template.base.Template'>

    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 2
0
def get_page_or_404(name):
    """
    Return page content as a Django template or raise 404 error.
    """
    try:
        file_path = safe_join(settings.SITE_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') as f:
        page = Template(f.read())

    meta = None

    # loops through the page’s raw nodelist,
    # and check for a BlockNode with the name context.
    # BlockNode is a class definition for creating {% block %} in Django templates.
    # If the context BlockNode is found,
    # we defines a meta-variable for us that contains that content.
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break

    page._meta = meta
    return page
Esempio n. 3
0
def get_page_or_404(name):
    """Return page content as a Django template or raise 404 error."""
    try:
        # Uses safe_join to join the page’s file path with the template’s filename and returns a normalized,
        # absolute version of the final path.
        file_path = safe_join(settings.SITE_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') as f:
        # Opens up each file and instantiates a new Django template object with its contents.
        page = Template(f.read())
    meta = None

    # 此代码循环遍历页面的原始节点列表,并检查名称为context的BlockNode。
    # BlockNode是用于在Django模板中创建{%block%}元素的类定义。
    # 如果找到上下文BlockNode,它会为我们定义包含该内容的元变量。
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 4
0
def get_page_or_404(name):

    try:
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY,name)
        print(file_path)
    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') as f:
        page = Template(f.read())


    meta = None
 
    for i,node in enumerate(list(page.nodelist)): # crea una tupla (1,e1) , (2,e2) 
        if isinstance(node,BlockNode) and node.name =='context':
            meta = page.nodelist.pop(i)
            break

    print(type(page.nodelist))
    page._meta = meta # Add la propiedad _meta que es un Node 

    return page
Esempio n. 5
0
def get_page_or_404(name):
    """Return page content as a Django template or raise 404 error."""
    # print('page requested:', name)
    try:
        # safe join returns normalized, absolute file path
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
    except ValueError:
        # print('ValueError raised')
        raise Http404('Page Not Found')
    else:
        if not os.path.exists(file_path):
            print("{} doesn't exist".format(file_path))
            raise Http404('Page Not Found')
    # instantiate a new template with new file
    with open(file_path, 'r') as f:
        page = Template(f.read())

    # find all the block template tags    
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 6
0
def get_page_or_404(name):
    """ Return page content as a Django template or raise 404 error """
    try:
        file_path = safe_join(settings.SITE_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') as f:
        page = Template(f.read())
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 7
0
def get_page_or_404(name):
	try:
		file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
	except ValueError:
		raise Http404('Página não encontrada')
	else:
		if not os.path.exists(file_path):
			raise Http404('Página não encontrada')

	with open(file_path, 'r') as arquivo:
		page = Template(arquivo.read())
	meta = None
	for i, node in enumerate(list(page.nodelist)):
		if isinstance(node, BlockNode) and node.name == 'context':
			meta = page.nodelist.pop(i)
			break
	page._meta = meta
	return page
Esempio n. 8
0
def get_page_or_404(name):
    """Return page content as a Django template or raise 404 error"""
    try:
        file_path = safe_join(settings.SITE_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') as f:
        page = Template(f.read())
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 9
0
def get_page_or_404(name):
    # Retorna o conteudo da pagina ou gera o erro 404
    try:
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
    except ValueError:
        raise Http404('Pagina nao encontrada 1')
    else:
        if not os.path.exists(file_path):
            raise Http404('Pagina nao encontrada 2')
        # open (filename, mode) r=read, w=witring, a=append,
    with open(file_path, 'r') as f:
        page = Template(f.read())
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 10
0
def get_page_or_404(name):
    '''Return page content as a Django template or raise 404 error.'''
    try:
        file_path = os.path.join(settings.SITE_PAGES_DIRECTORY, name)
    except ValueError as e:
        raise Http404('Page not found (Value error: ' + e.reason + '.) ' +
                      'Path=' + file_path)
    else:
        if not os.path.exists(file_path):
            raise Http404('Page not found (Nonexistent path)')
    with open(file_path, 'r', encoding='utf-8') as f:
        page = Template(f.read())
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 11
0
def get_page_or_404(name):
    """  使用Django模板技术返回页面地址或者抛出404错误 """

    try:
        file_path = safe_join(settings.SITE_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') as f:
        page = Template(f.read())

    #遍历页面的原始节点列表,检查名称为context的BlockNode,也就是找到名为context的{% block %}块,它会定义一个元变量供我们包含内容
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 12
0
def get_page_or_404(name):
    try:
        file_path = safe_join(settings.SITE_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') as f:
        page = Template(f.read())
    """
    将 html中的{% block context %}这种BlockNode数据先pop出来,再用render,序列化后填加到context中
    本来是在python代码中 context词典中写的数据,现在可以用json形式写到html中了。
    """
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == "context":
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page
Esempio n. 13
0
def get_page_or_404(name):
	"""Return page content as a Django template or raise 404 page"""
	try:
		# safe_join joins the page's file path with the templates
		# filename and returns a normalized, absolute version of the final path
		file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
	except ValueError:
		if not os.path.exists(file_path):
			raise Http404('Page Not Found')

	with open(file_path, 'r') as f:
		# opens up each file and instantiates a new
		# Django template object with its content
		page = Template(f.read())

	meta = None
	for i, node in enumerate(list(page.nodelist)):
		if isinstance(node, BlockNode) and node.name == 'context':
			meta = page.nodelist.pop(i)
			break
	page._meta = meta

	return page
def get_page_or_404(name):
    """Return page content as a Django template or raise a 404 error"""
    try:
        file_path = safe_join(settings.SITE_PAGES_DIRECTORY, name)
    except ValueError:
        raise Http404('Page Not Found')
    else:
        if not os.path.exists(file_path):
            raise Http404('Page Does Not Exist')
    with open(file_path, 'r') as f:
        page = Template(f.read())
    """
    This code loops through the page's raw nodelist and checks for a BlockNode
    with the name context. BlockNode is a class definition for creating {% block %}
    elements in Django templates. If the context BlockNode is found, it defines a
    metavariable for us that contains the context (57).
    """
    meta = None
    for i, node in enumerate(list(page.nodelist)):
        if isinstance(node, BlockNode) and node.name == 'context':
            meta = page.nodelist.pop(i)
            break
    page._meta = meta
    return page