0

我正在尝试传递值(如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'

我究竟做错了什么?

请原谅我的新手。

4

1 回答 1

2

您始终可以从视图中传递它们:

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.
    })

希望能帮助到你!

于 2013-07-29T21:33:15.970 回答