简体   繁体   English

Django将数据从模板传递到查看url分配的问题

[英]issues with Django Passing Data from Template to View url dispatch

Having the below error while trying to dispatch two variables from my template to my view via url dispatching. 尝试通过url调度将两个变量从模板调度到我的视图时出现以下错误。 I dont understand why the url is not able to be found. 我不明白为什么无法找到该网址。 This is a snippet of my entire code, but I am pretty sure that the issue lies in the below. 这是我整个代码的片段,但是我很确定问题出在下面。 This is within an application within my project 这是我项目中的一个应用程序

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 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 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 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 for base project 用于基础项目的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),    
]

I was calling the url name of patient_summary from another location so thats why I kept getting the error. 我从另一个位置调用Patient_summary的URL名称,因此这就是为什么我不断收到错误消息的原因。 i think rule of thumb is always use relative url paths 我认为经验法则总是使用相对网址路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM