我试图在我的Django(1.10)项目中使用django-autocomplete-light 3.2.1和python(3.5),我发现使用它有很大的困难。然后我尝试按照教程中显示的实时演示,将所有文件下拉到我的本地驱动器进行复制(如http://dal-yourlabs.rhcloud.com/select2_outside_admin/上的那个),但是徒劳无功。对我来说,我想让我的本地演示工作,以便我可以基于此工作我自己的。
具体来说,当我按照以下代码运行网站时,会显示NoReverseMatch at /select2_outside_admin/
的错误,其中包含Reverse for 'linked_data' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
的说明我不知道出了什么问题。我附上了如下代码,如果您知道解决方案,请告知如何操作。非常感谢:
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'select2_outside_admin',
# Autocomplete
'dal',
# Enable plugins
'dal_select2',
'dal_queryset_sequence',
]
urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from .views import UpdateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
# url(r'^select2_outside_admin/', include('select2_outside_admin.urls')),
url(
r'^select2_outside_admin/$',
UpdateView.as_view(),
name='select2_outside_admin',
),
]
models.py:
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class TModel(models.Model):
name = models.CharField(max_length=200)
test = models.ForeignKey(
'self',
null=True,
blank=True,
related_name='related_test_models'
)
owner = models.ForeignKey(
'auth.user',
null=True,
blank=True,
related_name='owned_linked_models',
)
for_inline = models.ForeignKey(
'self',
null=True,
blank=True,
related_name='inline_test_models'
)
def __str__(self):
return self.name
forms.py:
from dal import autocomplete
from django import forms
from .models import TModel
class TForm(forms.ModelForm):
def clean_test(self):
owner = self.cleaned_data.get('owner', None)
value = self.cleaned_data.get('test', None)
if value and owner and value.owner != owner:
raise forms.ValidationError('Wrong owner for test')
return value
class Meta:
model = TModel
fields = ('name', 'owner', 'test')
widgets = {
'test': autocomplete.ModelSelect2(url='linked_data',
forward=('owner',))
}
class Media:
js = (
'linked_data.js',
)
views.py:
from django.shortcuts import render
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from select2_outside_admin.forms import TForm
from select2_outside_admin.models import TModel
# Create your views here.
class UpdateView(generic.UpdateView):
model = TModel
form_class = TForm
template_name = 'select2_outside_admin.html'
success_url = reverse_lazy('select2_outside_admin')
def get_object(self):
return TModel.objects.first()