I am learning about Django forms and am struggling to render a basic 'results' page for the form.
我正在学习Django表单,并且正在努力为表单呈现一个基本的“结果”页面。
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
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
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
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
mapping_twitter / results.html
<!--- DRAFT --->
Display search results here
1
If your results
endpoint is in an app called mapping_twitter
, then you can get at it with reverse('mapping_twitter:results')
.
如果您的结果端点在一个名为mapping_twitter的应用程序中,那么您可以使用反向操作(“mapping_twitter:results”)。
Further reading on reversing namespaced URLs: https://docs.djangoproject.com/en/2.0/topics/http/urls/#reversing-namespaced-urls
进一步阅读关于反向名称空间的url: https://docs.djangoproject.com/en/2.0/topics/http/urls/# rever- namespace -url。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/06/01/6ebb52b937a6d6d15b6ded6f8d95e023.html。