2017-03-05 118 views
1

我想在我的Postgresql数据库中保存HTML代码,以便直接发送此代码作为HttpResponse而不是提供静态HTML文件。我确实将它保存在数据库中,但以下错误不断出现。 Error after returning html codeDjango:在数据库中保存HTML代码的正确方法

做这个任务的正确方法是什么?

EDIT1:views.py文件

def index(request): 

    index_object = StaticWebPage.objects.get(page_title='index') 

    return HttpResponse(
     request, 
     index_object.page_content 
    ) 
+0

请(如果可以的话)粘贴您用作视图的代码。 – McAbra

+0

@McAbra好的等待发布代码 – aVIRA

+0

@McAbra添加了views.py代码 – aVIRA

回答

2

你们是不是要做出一个模板后端,将让您的模板在数据库中,而不是在文件系统中的代码?如果是这样的话:https://docs.djangoproject.com/en/1.10/topics/templates/#custom-backends

我可以看到在那个StaticWebPage记录中你有一个Django模板,为了呈现你需要处理的字符串和呈现,就像你在呈现字符串时一样。

from django.template import Template, Context 

def index(request): 
    index_object = StaticWebPage.objects.get(page_title='index') 
    template = Template(index_object.page_content) 
    return HttpResponse(
     content=template.render(Context({})), # use the context or not 
     content_type=None, 
     status=200, 
     reason=None, 
     charset=None, 
    ) 

(在Django中1.10)django.http.response.HttpResponse不采取request作为第一个参数(I加入它需要供参考参数)。

+0

此代码无法正常工作。 – aVIRA

+0

@aVIRA请详细说明,只有在对URL进行GET请求后,我才能够帮助您 – McAbra

+0

,输出为无。 – aVIRA

相关问题