Django 版本 1.8.16 pyodbc 版本:3.0.11b16
我一直在尝试为项目制作视图/提交表单。我需要实现的基本目标是使用 MS SQL Server 2014 中的存储过程查看和编辑/保存表单中的数据。我可以在视图页面中使用存储过程,但无法通过编辑来执行此操作项目并添加一个新项目。
模型.py
class procedures():
def view_patientsp(self, patid):
cursor = connection.cursor()
ret = cursor.execute("EXEC PR_PRES_viewpatient @uid=? ", (patid))
cursor.close()
return ret
class Patient(models.Model):
patientid = models.AutoField(db_column='PatientID', primary_key=True)
pyear = models.DecimalField(db_column='Pyear', max_digits=10, decimal_places=0, blank=True, null=True)
dref = models.DecimalField(db_column='DRef', max_digits=10, decimal_places=0, blank=True, null=True)
title = models.TextField(db_column='Title', blank=True, null=True)
fname = models.TextField(db_column='FName', blank=True, null=True)
lname = models.TextField(db_column='LName', blank=True, null=True)
dob = models.DateTimeField(db_column='DOB', blank=True, null=True)
pamonth = models.TextField(db_column='PAMonth', blank=True, null=True)
payear = models.TextField(db_column='PAYear', blank=True, null=True)
padays = models.TextField(db_column='PADays', blank=True, null=True)
sex = models.TextField(db_column='Sex', blank=True, null=True)
视图.py
def view_patient(request):
if request.method == 'POST':
form = viewpatientform(request.POST)
return render(request, 'lis/view.html', {'form': form})
else:
form = viewpatientform()
if form.is_valid():
procedure = procedures()
ret = procedure.view_patientsp(request.POST['fields'])
return render_to_response('lis/view.html', {'form': form})
网址.py
urlpatterns = [
url(r'^$', views.pat_list, name='index'),
url(r'^view/(?P<patid>\d+/)$', views.view_patient, name='viewpatient'), ]
视图.html
{% block body %}
{% load materializecss %}
{{ form|materializecss }}
<button type="submit" class="btn btn-primary">Submit</button>
{% endblock %}