python - Django URL error when using forms -
i new django , totally stuck on causing error. have done lots of searching no avail! super appreciated.
the actual form works fine when try , submit input data error:
using urlconf defined in mysite.urls, django tried these url patterns, in order: ^admin/ ^$ [name='home'] ^patientlist [name='patient_list'] ^patientdetail/(?p<pk>\d+)/$ [name='patient_detail'] ^add_patient/$ [name='add_patient'] current url, spirit3/add_patient/, didn't match of these.
my urls.py in mysite directory looks like:
from django.conf.urls import url django.contrib import admin django.conf.urls import include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'', include('spirit3.urls')), ]
my urls.py in app looks like:
from django.conf.urls import url . 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'), ]
the relevant part of 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})
and html looks like:
{% extends 'spirit3/base.html' %} {% block content %} <body> <h1> add patient </h> <form action="/spirit3/add_patient/" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="create patient" /> </form> </body> {% endblock %}
thanks in advance! :)
the form "action" attribute wrong... seeing urls configuration dont have /spirit3/add_patient/
url, think /add_patient/
or use form tag without "action" post current page:
<form role="form" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="create patient" /> </form>
hope helps
Comments
Post a Comment