Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用表单访问django模板中的m2m字段值_Python_Django_Django Forms_Django Templates - Fatal编程技术网

Python 使用表单访问django模板中的m2m字段值

Python 使用表单访问django模板中的m2m字段值,python,django,django-forms,django-templates,Python,Django,Django Forms,Django Templates,请帮助我使用表单访问模板中的m2m值 我的模型: class Opportunity(models.Model): sfdc_id = models.PositiveIntegerField(null=True, blank=True) class Event(models.Model): background = models.TextField(null=True, blank=True) account = models.ForeignKey(Account)

请帮助我使用表单访问模板中的m2m值

我的模型:

class Opportunity(models.Model):
    sfdc_id = models.PositiveIntegerField(null=True, blank=True)

class Event(models.Model):
    background = models.TextField(null=True, blank=True)
    account = models.ForeignKey(Account)
    opportunities = models.ManyToManyField(Opportunity, null=True, blank=True)
Views.py

def event(request,event_id):
    if request.method == 'GET' :
        eventForm=EventForm(instance=Event.objects.get(pk=event_id))
        locationForm = LocationForm(prefix='location')
        return render(request,'events/event.html', {'eventForm': eventForm, 'locationForm': locationForm})
更新了模型表格:

class EventForm(ModelForm):
    account = ModelChoiceField(queryset=Account.objects.all())
    background = CharField(widget=Textarea(attrs={'class':'form-textarea resizeoff auto-resize js_auto_resize  default'}), required=False)
    opportunities = CharField(label='SalesForce ID', widget=TextInput(attrs={'class' : 'form-input'}), required=False)
在我的模板中,我尝试了以下代码,但不起作用

{% for attr in eventForm.opportunities.all %}
      <td>{{ attr.sfdc_id }}</td>
{% endfor %}
{% for attr in eventForm.opportunities_set.all %}
      <td>{{ attr.sfdc_id }}</td>
{% endfor %}
{%for attr in eventForm.opportunities.all%}
{{attr.sfdc_id}
{%endfor%}
我已经试过了,但仍然不起作用

{% for attr in eventForm.opportunities.all %}
      <td>{{ attr.sfdc_id }}</td>
{% endfor %}
{% for attr in eventForm.opportunities_set.all %}
      <td>{{ attr.sfdc_id }}</td>
{% endfor %}
{%for attr in eventForm.opportunities\u set.all%}
{{attr.sfdc_id}
{%endfor%}
请帮帮我


在您的表格中,将Opportunity的定义更改为:

class EventForm(ModelForm):
    account = ModelChoiceField(queryset=Account.objects.all())
    background = CharField(widget=Textarea(attrs={'class':'form-textarea resizeoff auto-resize js_auto_resize  default'}), required=False)
    opportunities = ModelMultipleChoiceField(queryset=Opportinuty.objects.all(), label='SalesForce ID', required=False)
文件

这将呈现一个正确的M2M表单控件。 如果您希望将小部件更改为更可用的小部件,我建议您

它有一个我觉得非常有用(而且很好!)


您应该向我们展示EventForm和Location的定义。我已经更新了模型表单,请查看@Alvaro我想将opportunities字段保留为文本框,因为用户需要在编辑event form时为opportunities输入新值。我向您展示的小部件允许您动态添加新值,但是如果你想使用文本字段,你需要手动处理表单是的,我正在手动处理这些值,这是我手动处理的代码。然后我将添加一个上下文变量:opportunities_list=opportunities.split(',');并渲染它manually@Alvaro正如您所建议的,我已将视图中的opportunities_列表作为上下文变量传递,并在模板中手动呈现!!