Django将数据从模板传递到查看URL调度的问题

时间:2018-02-14 18:53:33

标签: python django django-templates django-views

尝试通过url调度将我的模板中的两个变量分配到我的视图时出现以下错误。我不明白为什么网址无法找到。这是我整个代码的片段,但我很确定问题出在下面。这是在我的项目中的应用程序

NoReverseMatch at /home/patientid=1002411102/clinicid=1007/
Reverse for 'patient_summary' with no arguments not found. 1 pattern(s) tried: ['home/patientid=(?P<patient_id>\\d+)/clinicid=(?P<clinic_id>\\d+)/$']

的index.html

<tbody>
    {% for patient in patients %}
    <tr class="gradeX">
        <td>{{patient.PatientID}}</td>
        <td>{{patient.ClinicID}}</td>
        <td>{{patient.PatientFirstName}}</td>
        <td>{{patient.PatientMidName}}</td>
        <td>{{patient.PatientLastName}}</td>
        <td>{{patient.PatientDOB}}</td>
        <td>40/30</td>
        <td>192</td>
        <td>100</td>
        <td>70</td>
        <td>23m</td>
        <td style="color:red">Abnormal</td>
        <td><a style="color:#4A90E2; cursor: pointer" href="{% url 'home:patient_summary' patient_id=patient.PatientID clinic_id=patient.ClinicID %}">View/Edit</a></td>
    </tr>
    {% endfor %}
</tbody>

urls.py

from django.conf.urls import url
from home import views

app_name = 'home'

urlpatterns = [
    url('^$', views.index, name='index'),
    url('patientid=(?P<patient_id>\d+)/clinicid=(?P<clinic_id>\d+)/$', views.patient_summary, name='patient_summary'),
    ]

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template.context_processors import request
from django.contrib.auth.decorators import login_required
import requests
import json

@login_required
    def index(request):
        payload = {"ClinicId":"1007"}
        r = requests.post("https://hidden", data=payload)
        jsonList = r.text
        data = json.loads(jsonList)
        # print(data[0]["PatientID"])
        patients = []
        for patient in data:
             patients.append(patient)
        my_dict = {'patients': patients, 'homeIsActive': 'active'}
        return render(request, 'home/index.html', my_dict)

    def patient_summary(request, patient_id, clinic_id):
        my_dict = {'homeIsActive': 'active', 'chartData': [[0,1],[1,1],[2,1]]}
        return render(request, 'home/patient_summary.html', my_dict)

urls.py用于基础项目

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf.urls import url
from login import views as login
from home import views as home
from enrollment import views as enrollment
from inventory import views as inventory

urlpatterns = [
    url('^$', login.user_login, name='login'),
    url('^logout/', login.user_logout, name='logout'),
    url('^home/', include('home.urls')),
    url('^enrollment/', enrollment.index, name ='enrollment'),
    url('^inventory/', inventory.index, name ='inventory'),
    # url('^settings/', settings.index, name='settings'),
    url('^settings/', include('settings.urls')),
    path('admin/', admin.site.urls),    
]

1 个答案:

答案 0 :(得分:0)

我从另一个位置调用了patient_summary的url名称,这就是为什么我一直收到错误的原因。我认为经验法则总是使用相对URL路径