django if else 模板标签使用对象 from with template 标签

django if else template tag using object from with template tag

提问人:Bonny Clarke 提问时间:10/5/2023 最后编辑:Bonny Clarke 更新时间:10/5/2023 访问量:42

问:

我有一个 if/else 语句,用于确定哪个按钮将显示在我的 base.py 模板中。通过我的研究,我认为我走在正确的道路上,但无论 enddate 字段是 null 还是填充,都会显示 endcast 按钮。

型:

class Cast(models.Model):
    id = models.AutoField(db_column='Id')
    startdate = models.DateTimeField(db_column='StartDate')
    enddate = models.DateTimeField(db_column='EndDate')

    class Meta:
        managed = True
        db_table = 'Cast'
        verbose_name_plural = "Cast"
        get_latest_by = "pk"

模板块:

    <div class="top-button-container stickey-top">
        {% with last=Cast.objects.latest %}
            {% if last.enddate %}
                <button type="button" class="btn btn-danger btn-lg btn-block">
                    <a class="nav-link" href="{% url 'caststart' %}">Start Cast</a>
                </button>
            {% else %}
                <button type="button" class="btn btn-danger btn-lg btn-block">
                    <a class="nav-link" href="{% url 'cast_end' %}">End Cast</a>
                </button>
            {% endif %}
        {% endwith %}
    </div>

视图:

def caststart(request):
    context ={}
    form = StartCastForm(request.POST or None)
    if request.method == "POST":
        form = StartCastForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            castid=Cast.objects.last()
            return HttpResponseRedirect("%i/castend" % castid.pk)
    else:
        form = StartCastForm 
        if 'submitted' in request.GET:
            submitted = True
            return render(request, 'wwdb/casts/caststart.html', {'form':form, 'submitted':submitted, 'id':id})

    context['form']= form

    return render(request, "wwdb/casts/caststart.html", context)

def cast_end(request):
    last = Cast.objects.latest('pk')
    return redirect('castend', id=last.pk)

def castend(request, id):
    context ={}
    obj = get_object_or_404(Cast, id = id)
    form = EndCastForm(request.POST or None, instance = obj)
    if request.method == 'POST':
        cast=Cast.objects.last()
        if form.is_valid():
            form.save()
            cast.refresh_from_db()
            cast.endcastcal()
            cast.save()
            return HttpResponseRedirect("/wwdb/casts/%i/castenddetail" % cast.pk)
    context["form"] = form
    return render(request, "wwdb/casts/castend.html", context)

我期待在填充最后一个对象 enddate 字段时呈现“开始投射”按钮。当 enddate 字段为 none 时,“end cast”按钮应呈现。它目前呈现“结束投射”按钮,而不考虑最后一个对象的 enddate 字段值。

python html django 模板

评论

1赞 jaap3 10/5/2023
呈现此模板的视图是什么样子的?
0赞 Bonny Clarke 10/5/2023
我更新了问题以包含视图。@jaap3
0赞 jaap3 10/5/2023
似乎模板从未在上下文中传递模型,因此不执行任何操作。您可以在上下文中传递模型,也可以在视图中进行查找并传递该模型。CastCast.objects.latestCastlast
0赞 Bonny Clarke 10/5/2023
但这是基本模板,因此几乎每个模板都会加载此模板。我是否需要在与将加载我的 base.py 模板关联的每个视图的上下文中传递 Cast 模型?@jaap3
1赞 jaap3 10/5/2023
您可能想研究该 docs.djangoproject.com/en/4.2/ref/templates/api/ 的上下文处理器...

答:

0赞 Bonny Clarke 10/5/2023 #1

最后,我需要添加一个上下文处理器来解决这个问题。

我创建了文件 myapp/context_processors.py

from myapp.models import Cast

def cast_context(request):
    return {'last': Cast.objects.last()}

在我的 base.py 模板中:

<div class="top-button-container stickey-top">
    {% if last.enddate %}
        <button type="button" class="btn btn-danger btn-lg btn-block">
            <a class="nav-link" href="{% url 'caststart' %}">Start Cast</a>
        </button>
    {% else %}
        <button type="button" class="btn btn-danger btn-lg btn-block">
            <a class="nav-link" href="{% url 'cast_end' %}">End Cast</a>
        </button>
    {% endif %}
</div>

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'context_processors.cast_context',
)

TEMPLATES = [
    {
        'OPTIONS': {
                'context_processors.cast_context',
        },
    },
]