Reputation:
I am learning about Django forms and am struggling to render a basic 'results' page for the form.
I would be greatly appreciative if somebody could point out what I'm doing wrong! Thanks in advance :)
ERROR
NoReverseMatch at /search_query/
Reverse for 'results' not found. 'results' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://ozxlitwi.apps.lair.io/search_query/
Django Version: 2.0
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'results' not found. 'results' is not a valid view function or pattern name.
Exception Location: /mnt/data/.python-3.6/lib/python3.6/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 632
Python Executable: /mnt/data/.python-3.6/bin/python
Python Version: 3.6.5
Python Path:
['/mnt/project',
'/mnt/data/.python-3.6/lib/python36.zip',
'/mnt/data/.python-3.6/lib/python3.6',
'/mnt/data/.python-3.6/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6',
'/mnt/data/.python-3.6/lib/python3.6/site-packages']
Server time: Fri, 1 Jun 2018 10:00:20 +0900
views.py
def search_query(request):
# If POST request, process the Form data:
if request.method == 'POST':
# Create a form instance and populate it with the data from the request (binding):
form = SearchQueryForm(request.POST)
# Check if the form is valid:
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('results'))
else:
form = SearchQueryForm()
context = {'form':form}
return render (request, 'mapping_twitter/search_query.html', context)
def results(request):
context = {'form':form}
return render(request, 'mapping_twitter/results.html', context)
mapping_twitter/urls.py
from django.urls import path
from . import views
app_name = 'mapping_twitter'
urlpatterns = [
path('', views.search_query, name='search-query'),
path('results/', views.results, name='results'),
]
mapping_data/urls.py
urlpatterns = [
path('search_query/', include('mapping_twitter.urls')),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='/search_query', permanent=True)),
]
mapping_twitter/results.html
<!--- DRAFT --->
Display search results here
Upvotes: 2
Views: 128
Reputation: 3755
If your results
endpoint is in an app called mapping_twitter
, then you can get at it with reverse('mapping_twitter:results')
.
Further reading on reversing namespaced URLs: https://docs.djangoproject.com/en/2.0/topics/http/urls/#reversing-namespaced-urls
Upvotes: 1