Check if a form exists or is rendered in Template.

2019-08-19 00:37发布

问题:

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.

The template code

{% 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 %}

This is the view code

@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')

回答1:

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)