我正在尝试传递值(如下面的view
中所定义),以便在模板中使用。
查看
from Scanner.forms import SubmitDomain
from django.http import HttpResponse
from django.shortcuts import render
from django.template.context import RequestContext
#from django.template import loader
def index(request):
return HttpResponse('<h1>Page was found</h1>')
def Scan(request):
## Setup Template.
return render(request, 'VA/index.html',
{'pagetitle': 'Home'},
{'container_content' : 'Testing some content dude!'},
##{'form' : 'form'},
context_instance=RequestContext(request, processors=[]))
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
form.cleaned_data['domainNm'] ## clean data in dictionary
form.save()
我在模板中使用以下内容:
<title>Website Name - {% block pagetitle %}{% endblock pagetitle %}</title>
{% block container_content %}{% endblock container_content %}
错误我得到了:
ender_to_string() got multiple values for keyword argument 'context_instance'
我做错了什么?
请原谅我的新手。
答案 0 :(得分:1)
您始终可以从视图中传递它们:
def your_view(request):
# .... your code here ....
return render(request, 'VA/index.html', {
'form' : form,
'page_title': 'My page',
'meta_description': 'Some desc'
})
在你的模板中:
<meta name="description" content="{{ meta_description }}">
<title>Website Name - {{ page_title }}</title>
然而,这似乎并不是真的干,因为你必须在每个视图和每个模板中重复它。所以我要做的是在基本模板中创建具有一些默认值的块,例如base.html
:
<meta name="description" content="{% block blk_metadesc %} Some default meta here {% endblock blk_metadesc %}">
<title>Website Name - {% block blk_pagetitle %}Default title{% endblock blk_pagetitle %}</title>
然后,您可以在子模板中覆盖它们,例如child.html
:
{% extends "base.html" %}
{% block blk_metadesc %} {{ meta_description }} {% endblock blk_metadesc %}
{% block blk_pagetitle %} {{ page_tile }} {% endblock blk_pagetitle %}
因此,在您的子模板中,您不必一遍又一遍地执行相同的操作,但如果您愿意,则只覆盖块中的默认值。
修改强>
您的观看代码非常混乱:
def Scan(request):
## Setup Template.
return render(request, 'VA/index.html', # after you do 'return' in a function, the following code won't run
{'pagetitle': 'Home'},
{'container_content' : 'Testing some content dude!'}, # this line caused the error, you can pass only ONE dict as the argument.
##{'form' : 'form'},
context_instance=RequestContext(request, processors=[])) # you don't really need this context_instance at all.
# the following code won't run
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
form.cleaned_data['domainNm'] ## clean data in dictionary
form.save()
我会像这样改写那个观点:
def Scan(request):
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
form.cleaned_data['domainNm'] ## clean data in dictionary
form.save()
return render(request, 'VA/index.html', {
'pagetitle': 'Home',
'container_content' : 'Testing some content dude!',
'form' : 'form' # comment out this line if you don't need the form.
})
希望它有所帮助!