【问题标题】:how to set variable through views in django如何通过django中的视图设置变量
【发布时间】:2014-11-21 08:10:33
【问题描述】:

如何在视图中设置任何变量,以便我可以在模板中获取它的值

home.html

<html>
<head>
    <title>To-Do lists</title>
</head>
<body>
    <h1>Your To-Do list</h1>
    <form method="POST">
    <input id="id_new_item" name="item_text" placeholder="Enter a To-Do item"/>
    {% csrf_token %}
    </form>
    <table id="id_list_table">
        <tr><td>1 : {{new_item_text}}</td></tr>
    </table>
</body>

views.py

from django.shortcuts import render
from django.http import HttpResponse
from lists.models import Item

def home_page(request):
    if request.method == 'POST':
        new_item_text = request.POST['item_text'] #
        print new_item_text;
        Item.objects.create(text=new_item_text) #
    else:
        new_item_text = '' #
    item = Item()
    item.text = request.POST.get('item_text', 'A new list item')
    item.save()
    return render(request, 'home.html', {
        'new_item_text': new_item_text
    })

模型.PY

from django.db import models

class Item(models.Model):
    text = models.TextField(default='')

这是失败的测试
测试.py

def test_home_page_returns_correct_html(self):
    request = HttpRequest() #
    response = home_page(request) #
    self.assertIn('A new list item', response.content.decode())
    expected_html = render_to_string('home.html',
        {'new_item_text': 'A new list item'}
    )
    self.assertEqual(response.content.decode(), expected_html)

错误是

AssertionError: 'A new list item' not found in u'<html>\n\t<head>\n\t\t<title>To-Do lists</title>\n\t</head>\n\t<body>\n\t\t<h1>Your To-Do list</h1>\n\t\t<form method="POST">\n\t\t<input id="id_new_item" name="item_text" placeholder="Enter a To-Do item"/>\n\t\t\n\t\t</form>\n\t\t<table id="id_list_table">\n\t\t\t<tr><td>1 : </td></tr>\n\t\t</table>\n\t</body>\n</html>\n'

请告诉我我提出的问题的解决方案或任何材料(任何链接)

【问题讨论】:

  • 好的。那么当您尝试此代码时会发生(或未发生)什么? (为什么要创建两次该项目?)
  • @DanielRoseman 我不明白你在括号中说什么。你能详细说明吗?
  • 您在 POST 块中调用 Item.objects.create(...),这会创建一个项目,然后您实例化 另一个 新项目并保存它。为什么?
  • 但这与您的测试失败无关。您的测试没有将“新列表项”传递给 request.POST,所以我看不出您期望它如何工作。
  • @DanielRoseman 实际上我是 django 新手,我想设置变量,我想在模板中设置变量“new_item_text”,所以建议我使用任何有效的代码示例,其余代码是虚拟的

标签: python django django-templates


【解决方案1】:

模板使用一些上下文数据进行渲染,这是您可以传递变量的地方。对于 Imran 建议的基于类的视图,您可以从

get_context_data method

在函数视图中,您可以将上下文传递给渲染快捷方式

return render(request, 'my-template.html', {"variable1": True, "variable3": foo})

【讨论】:

  • 我在模板中写入 {{variable1}} 但没有任何内容
  • 你能用你的视图代码和模板的 sn-p 更新问题吗?
【解决方案2】:

你应该使用基于类的视图

class HomeView(TemplateView):
template_name = 'home.html'

def get_context_data(self, **kwargs):
    context = super(HomeView, self).get_context_data(**kwargs)
    context['user']= self.request.user.username
    return context

在视图中您可以简单地访问用户

{{user}}

【讨论】:

  • 这不是一个真正的答案。这个问题一开始就提出不好,但这里没有理由推荐基于类的视图而不是功能视图。
  • @DanielRoseman 我应该给出我目前面临的情况吗?
  • @sagar 当然。所有问题都应包含足够的上下文,以及相关代码和对无效内容的完整描述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
  • 2020-11-11
  • 2016-11-09
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多