相关疑难解决方法(0)

如何将参数传递给Django中模板变量的方法?

我正在使用lxml库在视图中定义变量(类别).lxml提供了一种.get检索自定义属性的方法.我想在模板中使用它,如下所示:

{{ category.get("foo") }} 
Run Code Online (Sandbox Code Playgroud)

我知道使用模板变量时不必使用括号,但是我收到以下错误:

{{ category.get "foo" }}
Run Code Online (Sandbox Code Playgroud)

无法解析余数:''foo''来自'category.get"foo"'

我假设我的语法有问题,但谷歌一直没有帮助.django文档说通过使用a来查找方法.

django templates

23
推荐指数
3
解决办法
3万
查看次数

将参数传递给Django模板中的模型方法

我正在尝试从指定的业务中获取所有客户,但在Customer模型中有一个类方法来获取基于相同业务的一些信息...让我用代码解释一下......

class Business(models.Model):
    ...
    customers = models.ManyToManyField(Customer, blank=True, null=True)

class Customer(models.Model):
    ...
    def get_something(self, obj_business)
        ...
Run Code Online (Sandbox Code Playgroud)

好的,现在在我的观点中,我从这样的指定业务中获得所有客户:

obj_customers = obj_business.customers.all()
Run Code Online (Sandbox Code Playgroud)

然后我尝试在我的模板中打印它:

{% for obj_customer in obj_customers %}
    {{ obj_customer.get_something ....... }}
Run Code Online (Sandbox Code Playgroud)

但是,是的,没有办法传递论据......我想知道是否有一些我遗漏的东西......

我想知道是否有另一个解决方案而不是创建模板标签...因为它是一个ManyToManyField,如果customers字段只是ForeignKey,则不需要将参数传递给该方法......

django

16
推荐指数
2
解决办法
2万
查看次数

TemplateSyntaxError:期望令牌':',得到'}'

这是我的源代码:

<ul class="filebase">
        {% for file in finance %}
        <li class="filelist">
            <div class="file-author"><a href="http://127.0.0.1:5000/uploads/files/{{file. filename}}">{{ file.filename }}</a></div>
            <div class="file-body">Description: {{ file.description }}</div>
            <div class="file-date">Date posted: {{ moment(file.date).fromNow() }}</div>
            <div class="delete-file"><a href="{{ url_for('.delete_file', file_name={{ file.filename }} ) }}">Delete File</a></div><br>
        </li>
        {% endfor %}
Run Code Online (Sandbox Code Playgroud)

起初,我的代码工作正常,我突然得到这样的错误:

TemplateSyntaxError:期望令牌':',得到'}'

这是我的追溯(如果你需要):

Traceback (most recent call last):
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line …
Run Code Online (Sandbox Code Playgroud)

python jinja2 flask

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

django ×2

flask ×1

jinja2 ×1

python ×1

templates ×1