[英]Include template with dynamic Django form
I have a model with a choices field, and the creation form has different fields depending on the field. 我有一个带有选择字段的模型,并且创建表单根据字段具有不同的字段。 I'm implementing the design of a dashboard in which the user can click a button (one button for each choice) and a model containing the model form will be displayed.
我正在实现仪表板的设计,在该仪表板中,用户可以单击一个按钮(每个选择一个按钮),然后将显示包含模型表单的模型。
I want to use a template only for the model and send the form and action URL to it depending on which button the user clicks, but that would mean I have to mix JS and Django variables and I don't know if that's a good idea. 我只想为模型使用模板,并根据用户单击的按钮向其发送表单和操作URL,但这意味着我必须混合使用JS和Django变量,并且我不知道这是一个好主意。
The code right now is something like this: 现在的代码是这样的:
dashboard.html dashboard.html
<div class="add-connections">
<div class="col-sm-3 text-center" >
<div class="connection-box">
{% if user_connections.EP %}
<img src="{% static 'images/green-check-mark.png' %}" alt="Connection added" class="check">
{% else %}
<a href="" class="btn" data-toggle="modal" data-target="#modal-new-connection">Activate</a>
{% endif %}
</div>
</div>
<div class="col-sm-3 text-center" >
<div class="connection-box">
{% if user_connections.OLX %}
<img src="{% static 'images/green-check-mark.png' %}" alt="Connection added" class="check">
{% else %}
<a href="" class="btn" data-toggle="modal" data-target="#modal-new-connection">Activate</a>
{% endif %}
</div>
</div>
<div class="col-sm-3 text-center" >
<div class="connection-box">
{% if user_connections.CC %}
<img src="{% static 'images/green-check-mark.png' %}" alt="Connection added" class="check">
{% else %}
<a href="" class="btn" data-toggle="modal" data-target="#modal-new-connection">Activate</a>
{% endif %}
</div>
</div>
<div class="col-sm-3 text-center" >
<div class="connection-box">
{% if user_connections.FR %}
<img src="{% static 'images/green-check-mark.png' %}" alt="Connection added" class="check">
{% else %}
<a href="" class="btn" data-toggle="modal" data-target="#modal-new-connection">Activate</a>
{% endif %}
</div>
</div>
</div>
{% url 'create_olx_connection' as olx_url %}
{% url 'create_ep_connection' as ep_url %}
{% url 'create_cc_connection' as cc_url %}
{% url 'create_fr_connection' as fr_url %}
{% include "properties/dashboard/connection_modal.html" %}
connection_modal.html connection_modal.html
<!-- Modal -->
<div class="modal fade modal-property" id="modal-new-connection" tabindex="-1" role="dialog" aria-labelledby="myConnectionModal">
<div class="modal-dialog" role="document">
<div class="modal-content panel-success">
<div class="modal-header panel-heading">
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-uppercase text-white"><b>Connections</b></h4>
</div>
<div class="modal-body">
<form action="{{ action_url }}"
{{ form.as_p }}
<div>
<button type="submit" class="btn btn-outline-success">Activate connection</button>
<a href="" class="btn">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
views.py views.py
def get_context_data(self, **kwargs):
context = super(
ManageServicesView, self).get_context_data(**kwargs)
...
context['connection_forms'] = {
'olx_form': OLXform(),
'el_pais_form': ElPaisForm(),
'finca_raiz_form': FincaRaizForm(),
'calicasa_form': CaliCasaForm()
}
return context
I want something like {% include "properties/dashboard/connection_modal.html" with form=chosen_form and action_url=chosen_url %}
but I don't know how to link each button's click event with Django variables to be able to do that. 我想要类似
{% include "properties/dashboard/connection_modal.html" with form=chosen_form and action_url=chosen_url %}
但我不知道如何将每个按钮的click事件与Django变量关联才能做到这一点。 I don't want to code a model for each option, I think it has to be a cleaner way. 我不想为每个选项编写模型,我认为它必须是一种更简洁的方法。 I'd appreciate your suggestions.
非常感谢您的建议。
You could put a {% for ... %} loop in connection_modal.html to generate all the modals, with the correct forms, that you need. 您可以在connection_modal.html中放入{%for ...%}循环,以生成所需模态的所有模态。 Than put a another for loop in dashboard.html to create the correct links.
比在dashboard.html中放置另一个for循环来创建正确的链接。
Tweaking views.py : 调整views.py :
context['connections'] = {
'OLX': {
'form' : OLXform(),
'user_connected': user_connections.OLX
},
'EP': {
'form' : ElPaisForm(),
'user_connected': user_connections.EP
},
# other connections here
}
dashboard.html: dashboard.html:
{% for connection,data in user_connected.items %}
<div class="col-sm-3 text-center" >
<div class="connection-box">
{% if data.user_connected %}
<img src="{% static 'images/green-check-mark.png' %}" alt="Connection added" class="check">
{% else %}
<a href="" class="btn" data-toggle="modal" data-target="#modal-new-connection-{{connection}}">Activate</a>
{% endif %}
</div>
</div>
{% endfor %}
connection_modal.html: connection_modal.html:
{% for connection,data in user_connected.items %}
{% if not data.connected %}
<!-- Modal for {{connection}} -->
<div id="modal-new-connection-{{connection}}" class="modal fade modal-property" tabindex="-1" role="dialog" aria-labelledby="myConnectionModal">
<!-- modal code before form -->
{{ data.form }}
<!-- modal code after form -->
</div>
{% endif %}
{% endfor %}
This should create as many modals as you need, each with their own id value, that can be called from the appropriate link. 这将创建所需数量的模态,每个模态都有自己的id值,可以从适当的链接中调用它们。 For example modal id "modal-new-connection-OLX" will be created with the OLX form and be called from the OLX link.
例如,模态ID“ modal-new-connection-OLX”将以OLX形式创建,并从OLX链接中调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.