【问题标题】:Pass Block Value to Template From View Django从视图 Django 将块值传递给模板
【发布时间】:2013-07-29 20:29:26
【问题描述】:

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

我做错了什么?

请原谅我的新手。

【问题讨论】:

    标签: django templates variables views block


    【解决方案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>
    

    但是,这似乎并不真正 DRY,因为您必须在每个视图和每个模板中重复它。所以我要做的是在基本模板中创建具有一些默认值的块,例如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.
        })
    

    希望对你有帮助!

    【讨论】:

    • 感谢您的解释,您能看看我提供的代码并告诉我我是否走在正确的轨道上吗?
    • 那是你的base.html 代码吗?如果是这样,看起来你是在正确的轨道上。请注意我回答中的默认值,它总有一天会对您有用。
    • 不,我还是看不到return函数Scan()的部分?!
    • 那是完整的文件,你的意思是叫form = SubmitDomain的表单的内容吗?
    • 关于您当前的答案,{% block pagetitle %} {{ page_tile }} {% endblock pagetitle %} "{{page_title}}" 来自哪里?这个值在哪里设置?
    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 2018-02-10
    • 1970-01-01
    • 2023-04-06
    • 2021-02-03
    • 2020-06-08
    • 2014-03-26
    • 2015-09-18
    相关资源
    最近更新 更多