本文介绍了检查表单是否存在或在模板中呈现。 Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会显示一个表单,有时却不显示它。

I have a situation where I display a Form sometimes and sometimes I don't display it.

实际上,有多个表单使用相同的提交按钮。

Actually, there are multiple forms using the same Submit button.

当模板中未显示特定表单时,我该怎么办以解决这种情况。

What do I do to take care of the situation when a particular form is not shown in the template.

模板代码

{% extends BASE_TEMPLATE %}
{%  load crispy_forms_tags %}
{% block title %}<h2>New Thread</h2>{% endblock %}
{% block content %}
    <div class="col-md-6">
        <form method="post" accept-charset="utf-8">{% csrf_token %}
            {{ threadForm|crispy }}
            {{ postForm|crispy }}
            {% if SHOW_WIKI %}
            {{ wikiFrom|crispy }}
            {% endif %}
            <input type="submit" class="btn btn-primary btn-sm" value="Submit"/>
        </form>
    </div>

{% endblock %}

这是视图代码

@login_required
def createThread(request, topic_title=None):
    if topic_title:
        try:
            if request.method == 'POST':
                topic = Topic.getTopic(topic_title)
                threadForm = ThreadSUForm(request.POST, prefix='thread')
                postForm = PostForm(request.POST, prefix='post')
                show_wiki = getattr(settings, "REFORUMIT_ALLOW_WIKI_FOR_THREADS", False) and topic.is_wiki_allowed
                wikiForm = WikiCreateForm(request.POST, prefix='wiki')

                if threadForm.is_valid() and postForm.is_valid() and wikiForm.is_valid():
                    thread = threadForm.save(commit=False)
                    post = postForm.save(commit=False)
                    wiki = wikiForm.save(commit=False)

                    thread.op = post
                    thread.wiki_revision = None

                    post.setMeta(request)
                    wiki.setMeta(request)


                    if is_authenticated(request):
                        post.created_by = request.user
                        wiki.author = request.user

                    thread.save()
                    wiki.wiki_for = thread
                    wiki.save()
                    post.save()

                    thread.wiki_revision = wiki

                    thread.save()


                    return HttpResponseRedirect(thread.get_absolute_url)
            else:
                topic = Topic.getTopic(topic_title)
                threadForm = ThreadSUForm(prefix='thread', initial={"topic": topic})
                postForm = PostForm(prefix='post')
                wikiForm = WikiCreateForm(prefix='wiki')
                show_wiki = getattr(settings, "REFORUMIT_ALLOW_WIKI_FOR_THREADS", False) and topic.is_wiki_allowed
            context = dict(threadForm=threadForm, postForm=postForm, wikiFrom=wikiForm, SHOW_WIKI=show_wiki)
            return render(request, 'reforumit/create_thread.html', context)
        except Topic.DoesNotExist:
            pass
    return redirect('topics')


推荐答案

这不会给出确切的答案,但是您可以稍微更改一下代码。

This won't give exact answer but you can change code a bit. Providing you if conditional section only.

if threadForm.is_valid() and postForm.is_valid():
    thread = threadForm.save(commit=False)
    post = postForm.save(commit=False)

    thread.wiki_revision = None

    thread.op = post
    post.setMeta(request)

    if is_authenticated(request):
        post.created_by = request.user
        post.save()

    thread.save()

    if wikiForm.is_valid():
        print("WikiForm is valid!")
        wiki = wikiForm.save(commit=False)
        print("Wiki has content")
        wiki.setMeta(request)
        if is_authenticated(request):
            wiki.author = request.user
        wiki.wiki_for = thread
        wiki.save()
        thread.wiki_revision = wiki
        thread.save()

    return HttpResponseRedirect(thread.get_absolute_url)

这篇关于检查表单是否存在或在模板中呈现。 Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:42