Autocompleting based on a List of Strings

24 views
Skip to first unread message

Mina Marmpena

unread,
Jun 14, 2019, 3:36:47 PM6/14/19
to yourlabs
Hello. I am not very experienced and I am struggling to get autocomplete to work. I need to use an autocomplete text input field  inside another form. 
I get NoReverseMatch: Reverse for 'emotion-autocomplete' not found. 


# views.py

from dal import autocomplete

class EmotionAutocomplete(autocomplete.Select2ListView):
 
def get_list(self):
 
return ['Sad', 'Happy', 'Angry', 'Neutral']


# This was my original view that worked fine before trying to get autocomplete for cat_emotion field

def
was_emotion(request):
   
if request.method == 'POST':
        form
= WasEmotionForm(request.POST)
       
if form.is_valid():
            attention
= form.cleaned_data['attention']
            was_emotion
= form.cleaned_data['likert_was_emotion']
            cat_emotion
= form.cleaned_data['cat_emotion']
            judge_obj
= Judge(attention=attention, was_emotion=was_emotion, cat_emotion=cat_emotion)

            judge_obj
.save()

               
return HttpResponseRedirect('/evaluation/')
   
else:

        form
= WasEmotionForm()

   
return render(request, 'wasEmotion.html', {'form': form})


# urls.py

urlpatterns = [
url
(r'^wasEmotion/$', views.was_emotion, name='was_emotion'),
...
url
(r'^emotion-autocomplete/$', views.EmotionAutocomplete.as_view(), name='emotion-autocomplete'),
]



# forms.py

def get_choice_list():
 
return ['Sad', 'Happy', 'Angry', 'Neutral']

class WasEmotionForm(forms.Form):
 attention
= forms.ChoiceField(label=" ", initial='1', widget=forms.RadioSelect, choices=(
 
('1', 'Strongly disagree'), ('2', 'Disagree'), ('3', 'Neither agree nor disagree'), ('4', 'Agree'),
 
('5', 'Strongly agree')))
 likert_was_emotion
= forms.ChoiceField(label=" ", initial='1', widget=forms.RadioSelect, choices=(
 
('1', 'Strongly disagree'), ('2', 'Disagree'), ('3', 'Neither agree nor disagree'), ('4', 'Agree'),
 
('5', 'Strongly agree')))

 cat_emotion
= autocomplete.Select2ListChoiceField(
 choice_list
=get_choice_list,
 widget
=autocomplete.ListSelect2(url='emotion-autocomplete'))

If I go to  127.0.0.1:8000/emotion-autocomplete/ view I see:

{"results": [{"text": "Sad", "id": "Sad"}, {"text": "Happy", "id": "Happy"}, {"text": "Angry", "id": "Angry"}, {"text": "Neutral", "id": "Neutral"}]}

But  127.0.0.1:8000/wasEmotion/  gives: 
Exception Value: Reverse for 'emotion-autocomplete' not found. 'emotion-autocomplete' is not a valid view function or pattern name.

I know I have done many things different from the example but I need to get this integrated in my previous code. Any help will be very much appreciated. 

James Pic

unread,
Jun 22, 2019, 6:04:56 AM6/22/19
to yourlabs
What is the path of your urls.py and settlings.ROOT_URLCONF ?

--
You received this message because you are subscribed to the Google Groups "yourlabs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yourlabs+u...@googlegroups.com.
To post to this group, send email to your...@googlegroups.com.
Visit this group at https://groups.google.com/group/yourlabs.
To view this discussion on the web visit https://groups.google.com/d/msgid/yourlabs/5ff89ca1-a74a-4a46-8939-05b2dae7020d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mina Marmpena

unread,
Jun 27, 2019, 8:07:13 AM6/27/19
to yourlabs
ROOT_URLCONF = 'galatea.urls'

My site is 'galatea' and my app is 'evalemo'


# galatea/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns
= [
 url
(r'^admin/', admin.site.urls),
 url
(r'^', include('evalemo.urls')),
]# evalemo/urls.py

app_name
= 'evalemo'


urlpatterns
= [
url
(r'^wasEmotion/$', views.was_emotion, name='was_emotion'),
...
url
(r'^emotion-autocomplete/$', views.EmotionAutocomplete.as_view(), name='emotion-autocomplete'),
]
To unsubscribe from this group and stop receiving emails from it, send an email to your...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages