在相关对象视图中创建oneToMany对象的视图

时间:2019-01-16 15:57:15

标签: django django-forms django-templates django-views

我有一个调查表,其中包含许多问题及其答案。我的主要目标是创建一个网页,在其中可以显示带有规则列表(问题/答案)的调查表的详细信息,并且在页面底部,我要调用“创建规则”页面:

def create_rule_view(request, id, sc_id):
if request.method == "POST":
    input = InputForm(request.POST)
    answer = AnswerForm(request.POST)
    rule = RuleForm(request.POST)
    if rule.is_valid() and input.is_valid() and answer.is_valid():
        r = rule.save()
        i = input.save(commit=False)
        a = answer.save(commit=False)
        i.rule_id = r
        i.save()
        a.rule_id = r
        a.save()
        question = input.cleaned_data["patterns"]
else:
    input = InputForm()
    answer = AnswerForm()
    rule = RuleForm()

return render(request, "rule/create_rule.html", {
    'rule': rule,
    'input': input,
    'answer': answer
})

def detail_scenario(request, id, sc_id):
    object = get_object_or_404(Scenario, id=sc_id)
    # TODO : add rule in the same view
    create_rule_div = create_rule_view(request, id, sc_id)
    print("content", create_rule_div)
    context = {
        'scenario': object,
        'create_rule_div': create_rule_div
    }
    return render(request, "scenario/detail_scenario.html", context)

这是rule_create.html:

{% block content %}

<form method="POST"> {% csrf_token %}
    <h2>Create Rule</h2>
    {{ rule.name}}
    {{ input.patterns }}
    {{ answer.text }}
    <input type="submit" value="Save Rule"/>

</form>
{% endblock %}

这是detail_senario.html:

{% block content %}


<h2>Scenario {{ scenario.id }}</h2>
<p>Scenario for : {{ scenario.chatbot_id }}</p>
<p>Name: {{ scenario.name }}</p>
<p>Description: {{ scenario.description }}</p>
<p>State: {{ scenario.state }}</p>
    {% for rule in scenario.rule_ids.all %}
        <li>{{ rule }}</li>
    {% endfor %}
    <div>{% block rule %}
    {{ create_rule_div.content }}{% endblock %}
    </div>
{% endblock %}

当我调用detail_scenario的url时,在导航器中得到了这样的html代码: html code in navigator 我怎样才能解决这个问题? 谢谢。

0 个答案:

没有答案