Error Message:*<br/><br/>
NoReverseMatch at /
Reverse for 'get_travel_details' with arguments '()' and keyword arguments '{u'origin': u'bus_terminals.Bus_Terminal_Id', u'destination': u'bus_terminals.Bus_Terminal_Id', u'travel_date': u'travel_date'}' not found. 1 pattern(s) tried: ['(?P< travel_date >\\w+)/travel_date/(?P< origin >\\w+)/origin/(?P< destination >\\w+)/destination']
我试图通过表单操作将值传递给url的参数,这就是我的表单标签在我的html文件中看到的内容:
<form method = "POST" action ="{% url 'brats:get_travel_details' travel_date='travel_date' origin='bus_terminals.Bus_Terminal_Id' destination='bus_terminals.Bus_Terminal_Id' %}" id = "find_travel_form">
然后我在项目文件夹中的urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^master/', admin.site.urls),
url(r'^', include('bus_reservation_system.urls')),
url(r'^(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination$', include('bus_reservation_system.urls')),
]
然后我在app文件夹中的urls.py:
from django.conf.urls import include, url
from . import views
app_name = "brats"
urlpatterns = [
url(r'^', views.index, name = "index"),
url(r'^(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination', views.get_travel_details, name = "get_travel_details"),
]
参数:travel_date,origin,destination应该是要传递的字符串值。
这是我的views.py看起来:
def get_travel_details(request, travel_date, origin, destination):
errors = []
if request.method == 'POST':
if not request.POST.get('travel_date', ''):
errors.append('Please determine your date of travel.\n')
if not request.POST.get('origin', ''):
errors.append('Please determine your point of origin.\n')
if not request.POST.get('destination', ''):
errors.append('Please determine your point of destination.\n')
if not errors:
all_bus = bus.objects.all()
elif errors:
travel_schedules = travel_schedule.objects.all()
bus_terminals = bus_terminal.objects.all()
bus_types = bus_type.objects.all()
data = {'travel_schedules': travel_schedules, 'bus_terminals': bus_terminals, 'errors': errors}
return render(request, 'pages/index.html', data)
答案 0 :(得分:0)
您的网址仍在传递字符串,其中包含.
,但您的正则表达式中并未包含该字符串。我怀疑你真的想要点并有一个字符串id所以只需删除引号..
所以改变
{% url 'brats:get_travel_details' travel_date='travel_date' origin='bus_terminals.Bus_Terminal_Id' destination='bus_terminals.Bus_Terminal_Id' %}
到
{% url 'brats:get_travel_details' travel_date='travel_date' origin=bus_terminals.Bus_Terminal_Id destination=bus_terminals.Bus_Terminal_Id %}
并确保Bus_Terminal_Id
没有任何非字字符
答案 1 :(得分:0)
<强> TL;博士强>
要修复错误,请从主网址中删除以下行:
url(r'^(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination$', include('bus_reservation_system.urls')),
<强>解释强>
您有两个指向同一个应用的网址。因此,每次你写{% url 'brats:get_travel_details' ...}
时,Django都会将其解析为最后一个注册的网址,在你的情况下是这样的:
url(r'^(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination$', include('bus_reservation_system.urls')),
最终解决了这个问题:
'(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination/(?P<travel_date>\w+)/travel_date/(?P<origin>\w+)/origin/(?P<destination>\w+)/destination'
正如您所看到的,最终解析的网址需要6个参数,但您只传递了3个参数。
这就是你收到错误的原因。