I there,
amazing community
I am at the moment building a vocabulary quizzing app - too much has been said about this before …
I have a FormView in which I want to handle the form, and I will share all of the necessary steps:
class WordDetailView(FormView):
'''shows the form to enter the meaning of the word.
Forwards to a random new word, once the word is answered correctly.
after three incorrect guesses, the word should be skipped - this needs to be implemented
'''
template_name = 'word_detail.html'
form_class = AnswerWord
#as I think I need to handle the success manually, here is None
success_url = None
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
word_id = self.kwargs.get('pk')
context['current_word'] = Word.objects.get(pk=word_id)
return context
def form_valid(self, form):
current_word_id = self.kwargs.get('pk')
current_word = Word.objects.get(pk=current_word_id)
if form.cleaned_data['translation'] == current_word.text:
random_word = Word.objects.exlcude(pk=current_word_id).order_by('?').first()
if random_word:
return redirect('word-detail', pk=random_word.pk)
else:
pass
#handling of incorrect answers still has to be implemented
return redirect('word-detail', pk=current_word_id)
Word-Model - in models.py:
class Word(models.Model):
'''The translated representations of each AbstractWord
word = the concept behind abstract
text = the word in the language we are looking for.
'''
word = models.ForeignKey(AbstractWord, to_field='abstract_word', on_delete=models.CASCADE)
language = models.ForeignKey(Language, on_delete=models.CASCADE)
text = models.CharField(max_length=100, blank=True)
definition = models.CharField(max_length=100, blank=True)
def __str__(self):
return f'{self.word}, {self.language}, {self.text}, {self.definition}'
the word_detail.html looks like this:
{% extends 'VocabTrainer/templates/base.html' %}
{% block content %}
<form method="post" action=" {% url 'word-detail' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Go</button>
</form>
{% endblock %}
when testing the form I receive this error message:
django.urls.exceptions.NoReverseMatch: Reverse for ‘word-detail’ with no arguments not found. 1 pattern(s) tried: [‘word\-list/(?P[^/]+)/(?P[0-9]+)\Z’]
The error message still does not find any patterns, if I add more arguments (there should be a current_word.pk, if I did not make a mistake :-D)
The code absolutely runs when I empty the action=" {% url 'word-detail' %}">
Can anyone give me a hint, where I should check and change anything? I think I have shared all the important code-snippets and models. I have a somewhat bad deprecatedWordDetailView(DetailView) which I can shaer later on, but this would obfuscate the issue further
I hope you can help me!
I thought I added the urls.py
urlpatterns = [
path('', IndexView.as_view(), name='index'),
path('word-list/', WordListView.as_view(), name='word-list-view'),
path('word-list/<str:language>/', LanguageWordListView.as_view(), name='lang-word-list-view'),
path('word-list/<str:language>/<int:pk>', WordDetailView.as_view(), name="word-detail"),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
All the best,