我對Django相當陌生,而且我完全停留在導致此錯誤的原因上。我做了很多搜索,但無濟於事!任何幫助將超級讚賞。Django使用表格時的URL錯誤
的實際形式工作正常,但是當我嘗試並提交輸入數據,我得到的錯誤:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home']
^patientlist [name='patient_list']
^patientdetail/(?P<pk>\d+)/$ [name='patient_detail']
^add_patient/$ [name='add_patient']
The current URL, spirit3/add_patient/, didn't match any of these.
我在mysite的目錄urls.py樣子:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('spirit3.urls')),
]
我的網址在的.py應用程序看起來像:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^patientlist', views.patient_list, name='patient_list'),
url(r'^patientdetail/(?P<pk>\d+)/$', views.patient_detail, name='patient_detail'),
url(r'^add_patient/$', views.add_patient, name='add_patient'),
]
views.py的相關部分:
def add_patient(request):
if request.method == 'POST':
form = PatientForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect('home')
else:
print form.errors
else:
form = PatientForm()
return render(request, 'spirit3/add_patient.html', {'form':form})
和HTML的樣子:
{% extends 'spirit3/base.html' %}
{% block content %}
<body>
<h1> Add a Patient </h>
<form action="/spirit3/add_patient/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create Patient" />
</form>
</body>
{% endblock %}
提前感謝! :)
僅僅因爲該應用程序被稱爲'spirit3'並不意味着URL使用它。你可以改變這個動作只是'/ add_patient /'儘管我不知道你在找什麼修正,但你可能想改變包含spirit3 url的URL(把它想象成字符串連接) – Sayse