【问题标题】:Render_to_response multiple args [duplicate]Render_to_response 多个参数 [重复]
【发布时间】:2015-08-03 07:27:07
【问题描述】:

我是 Django 新手,我正在尝试在能够编辑的网页上显示我的数据库。

这是我的观点.py

   def task1(request):
       if request.POST:
           form = AuthorForm(request.POST)
           if form.is_valid():
               form.save()
           return render(request,'tasks/index.html')
       else:
           form = AuthorForm()
       args = {}
       args.update(csrf(request))
       args['form'] = form
       return render_to_response('tasks/task1.html', {'authors':Author.objects.all()})

这样我可以在网页上打印我的数据库

但我也希望能够向其中添加新记录

我用过

return render_to_response('tasks/task1.html',args)

但是我怎样才能同时发送呢?据我了解,一定有字典。

这是一个模板

{% extends "tasks/index.html" %}

{% load bootstrap3 %}
{% load staticfiles %}
{% bootstrap_css %}
{% bootstrap_javascript %}

{% bootstrap_messages %}


{% block tasks %}

<p>31231</p>
<form action="/task1/" method="post">{% csrf_token %}
    <ul>
        {{ form.as_table}}
    </ul>
    <input type="submit" value="Create" />
</form>

{% if authors.count > 0 %}
    {% for author in authors %}
    <ul>
        {{author.id}}  {{author.name}} {{author.body}}
    </ul>
    {{% endfor %}
{{% endif %}
{% endblock %}

【问题讨论】:

    标签: python django


    【解决方案1】:
    args = {}
    args.update(csrf(request))
    
    args['form'] = form
    args['authors'] = Author.objects.all()
    return render_to_response('tasks/task1.html',args)
    

    【讨论】:

      【解决方案2】:
      return render_to_response('tasks/task1.html', {'authors':Author.objects.all(),'foo':foo,'bar':bar})
      

      args = {'authors':Author.objects.all(),'foo':foo,'bar':bar}
      return render_to_response('tasks/task1.html',args)
      

      请注意,您可以在 django >= 1.3 中简单地使用 render 而不是 render_to_response

      【讨论】:

        猜你喜欢
        • 2021-11-04
        • 2022-01-09
        • 2020-06-16
        • 2019-12-07
        • 2021-10-16
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多