我正在学习Django表格,并努力提供基本的结果'表单页面。
如果有人能够指出我做错了什么,我将非常感激!在此先感谢:)
错误
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
答案 0 :(得分:1)
如果您的results
端点位于名为mapping_twitter
的应用中,那么您可以使用reverse('mapping_twitter:results')
获取该端点。
有关撤消命名空间网址的进一步阅读:https://docs.djangoproject.com/en/2.0/topics/http/urls/#reversing-namespaced-urls