Django autocomplete指示灯:无输入字段

2024-04-27 03:55:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经安装了“Django autocomplete light”,现在按照这个教程:http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html

以下是我目前为止的代码:

在设置.py在

INSTALLED_APPS = [
    'dal',
    'dal_select2',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.gis',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
    'directory',
    'geopy',
]

在模型.py在

^{pr2}$

在视图.py在

class CompanyAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        if not self.request.user.is_authenticated():
            return Company.objects.none()

        qs = Company.objects.all()

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

在表单.py在

class AddCompanyForm(forms.ModelForm):
    vat = forms.CharField(required=True)
    class Meta:
        model = Company
        fields = ('name','city','address','telephone','email','vat','self_key')
        widgets = {
            'self_key': autocomplete.ModelSelect2(url='company-autocomplete')
        }
    def clean_vat(self):
        vat = self.cleaned_data['vat']
        if Company.objects.filter(vat__iexact=vat).count() > 1:
            raise ValidationError("L'attività digitata esiste già...")
        return vat

在网址.py在

url(
    r'^company-autocomplete/$',
    autocomplete.Select2QuerySetView.as_view(model=Company),
    name='company-autocomplete'
),

html格式

{% extends 'base.html' %}
{% load static %}

{% block title %}
  <title>Aggiungi azienda | ebrand directory</title>
{% endblock title %}

{% block upper_content %}
<br style="line-height:25px"/>
<div class="google-card-full" style="width:auto; height:auto; text-align:center;">
  <br style="line-height:25px"/>
  <h1 class='title'><img style="width:auto; height:auto;" src='{% static "images/new_company.png" %}'> Aggiungi azienda</h1>
  <hr/>
  <br/>
  <form method="post">
    {% csrf_token %}
    <table class='form-group' style="width:25%; margin:auto;">
      {% for field in form.visible_fields %}
        <tr>
          <th style='text-align:left'>{{ field.label|capfirst }}:</th>
          {% if forloop.first %}
            {% for hidden in form.hidden_fields %}
              {{ hidden }}
            {% endfor %}
          {% endif %}
          {{ field.errors.as_ul }}
          <td style='text-align:right'>{{ field }}</td>
        </tr>
      {% endfor %}
    </table>
    <br/>
    <button class="btn btn-raised btn-primary" type="submit"
            style="background-color:#1A88B9;">Aggiungi</button>
  </form>
  <p style="height:5px"/>
  <p><b>ATTENZIONE</b>: ebrand<sup>©</sup> si riserva di verificare l'autenticità dei dati<br/>
     prima dell'inserimento dell'azienda all'interno del database.</p>
  <p style="height:5px"/>
</div>
<h2 style='margin:0px 0px 15px 25px;'>Oppure...</h2>
<div class="google-card-full" style="width:auto; height:auto; text-align:center;">
  <br style="line-height:25px"/>
  <h1 class='title'><img style="width:auto; height:auto;" src='{% static "images/new_company.png" %}'> Reclama azienda esistente</h1>
  <hr/>
  <br/>
  <form method="post">
    {% csrf_token %}
    <table class='form-group' style="width:25%; margin:auto;">
      {% for field in form.visible_fields %}
        <tr>
          <th style='text-align:left'>{{ field.label|capfirst }}:</th>
          {% if forloop.first %}
            {% for hidden in form.hidden_fields %}
              {{ hidden }}
            {% endfor %}
          {% endif %}
          {{ field.errors.as_ul }}
          <td style='text-align:right'>{{ field }}</td>
        </tr>
      {% endfor %}
    </table>
    <br/>
    <button class="btn btn-raised btn-primary" type="submit"
            style="background-color:#1A88B9;">Aggiungi</button>
  </form>
  <p style="height:5px"/>
</div>
{% endblock %}

{% block middle_content %} {% endblock %}
{% block content %} {% endblock %}
{% block bottom_content %} {% endblock %}

问题是:“self_key”字段未填充,我也无法键入(不是输入)。在

我已经看了这个问题没有结果:Django autocomplete light: field not populated


Tags: djangotextbrselfformfieldautotitle