Py学习  »  MarcoBianchi  »  全部回复
回复总数  3
5 年前
回复了 MarcoBianchi 创建的主题 » 如何在Django模板中嵌套的if语句为true时立即结束forloop

我终于明白了。仍然在测试这段代码,它看起来到目前为止工作正常。如果有人能改进这段代码,我会很高兴的。因为循环在满足条件后仍在运行。这个 |slice:":1" 不起作用

{% for app in applications %}  
    {% if applicant.username in app.user.username %} #drawback of this code the loop runs even after the condition is met. No way in Django template to stop that. See my 2nd answer the code is better but logic run in views
    <p> Do A </p>
    {% endif %}
{% empty %}
     <p> Do B <p>
{% endfor %}
5 年前
回复了 MarcoBianchi 创建的主题 » 如何在Django模板中嵌套的if语句为true时立即结束forloop

这就是我在视图中得到它的方式,尽管现在太迟了明天会检查

def get_context_data(self, **kwargs):
    context = super(CreateApplication, self).get_context_data()
    context['applicant'] = self.request.user
    context['applications'] = Application.objects.all() #Application.objects.filter(is what I have used though)
    already_applied = None
    for app in Application.objects.all(): #Application.objects.filter(is what I have used though)
        if self.request.user.username in app.user.username:
            already_applied = self.request.user
            already_applied.save()
            print(already_applied)
            break
    context['already_applied'] = already_applied
    return context

下面是模板

{% if applicant == already_applied %}
    <p> Do A </p>
{% else %}
    <p> Do B </p> 
{% endif %}
4 年前
回复了 MarcoBianchi 创建的主题 » 获取403错误在cors-origin-django-cors头中添加aws静态文件路径

在花了一整天的时间来完成这项工作之后。我终于找到了一个带有SummerNote字体的cdn,之后我就不用担心cors了

在我的django模板中,只添加了下面的代码

    <style>
      @font-face {
            font-family: "summernote";
            font-style: normal;
            font-weight: normal;
            src: url("https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/font/summernote.eot");
            src: url("https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/font/summernote.eot") format("embedded-opentype"),
            url("https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/font/summernote.woff") format("woff"),
            url("https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/font/summernote.ttf") format("truetype");
        }
    </style>

您可以通过此链接获取cdn https://cdnjs.com/libraries/summernote