Django 列表 ID 到表单
Django List ID to Form
如果有人问这个问题,我很抱歉,但我找不到任何可以帮助我解决小问题的东西。
我已经设法创建了一个 django 框架,并创建了一个模型和一个包含来自 csv 文件的数据的表单。
基本上我想做的是:
我创建了一个“主页”,看起来像这样(它只是理解这个想法的基础 运行):
其中包含来自 model/form 的 IDS(带用户名)列表。
我希望能够做的是,当我点击其中一个 ID 时,它会打开我的表单页面,该页面仅与该 ID 的数据相关...我希望这是有道理的。
假设我创建了一个 form.html 文件,并且 ID 喜欢它以显示 form/model 中与我单击的那个 ID 相关的所有信息。
可能跟Href有关link?
如果我不太清楚,我深表歉意,但请随时问我任何问题,我会尽力提供帮助
这些是我的文件:
Model.py:
class datas(models.Model):
country = models.CharField(_('country'),max_length=200,default='Null')
qs_login = models.CharField(_('qs_login'),max_length=200,default='Null')
Status = models.CharField(_('Status'),max_length=200,default='Null')
seller_id = models.CharField(_('seller_id'),max_length=200,default='Null')
Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True)
associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null')
associate = models.CharField(_('associate'),max_length=200,default='Null')
metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null')
associate_action = models.CharField(_('associate_action'),max_length=200,default='Null')
correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null')
Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null')
Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null')
Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null')
QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null')
Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null')
investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null')
investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null')
investigator = models.CharField(_('investigator'),max_length=200,default='Null')
verification_action = models.CharField(_('verification_action'),max_length=200,default='Null')
correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null')
Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null')
Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null')
QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null')
General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null')
Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null')
def __str__(self):
return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"
#def __str__(self):
# return f"{self.country}-{self.qs_login}"
# super().save(*args,**kwargs)
Views.py:
def homePage(request):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(request,"main/home.html",context)
def auditFormPage(response):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(response,"main/auditform.html", context)
这是列表所在的 Home.html(如你所见,我将 item.Task_ID 和 item.qs_login:
{% for item in items %}
<div class="container-fluid">
<li class="nav-item dropdown">
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-items" href="#">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
</ul>
</li>
</div>
{% endfor %}
更新:
我尝试做的是在 views.py 页面中创建一个函数:
def auditFormPage(request,pk):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(request,"main/auditform.html", context)
然后我在 urls.py 文件中创建了一个路径,该文件采用动态 link 和主键:
path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),
并且在 dashboard/home 中我添加了一个 href link 到动态页面:
{% for item in items %}
<div class="container-fluid">
<li class="nav-item dropdown">
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
</ul>
</li>
</div>
{% endfor %}
但后来我收到 404 错误,因为找不到页面(我猜它正在寻找 AuditForm/str:pk/ 但只找到 AuditForm/):
Models.py:
class datas(models.Model):
country = models.CharField(_('country'),max_length=200,default='Null')
qs_login = models.CharField(_('qs_login'),max_length=200,default='Null')
Status = models.CharField(_('Status'),max_length=200,default='Null')
seller_id = models.CharField(_('seller_id'),max_length=200,default='Null')
Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True)
associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null')
associate = models.CharField(_('associate'),max_length=200,default='Null')
metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null')
associate_action = models.CharField(_('associate_action'),max_length=200,default='Null')
correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null')
Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null')
Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null')
Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null')
QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null')
Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null')
investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null')
investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null')
investigator = models.CharField(_('investigator'),max_length=200,default='Null')
verification_action = models.CharField(_('verification_action'),max_length=200,default='Null')
correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null')
Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null')
Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null')
QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null')
General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null')
Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null')
def __str__(self):
return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"
谢谢
更新答案
因为您 Task_ID
已设置 primary_key=True
。
path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),
为了使用传递给 auditFormPage
视图的 pk
从模型列表中检索特定对象,您可以在模型列表上使用 get
方法。
def auditFormPage(request, pk):
data= datas.objects.get(Task_ID=pk)
context = {'data': data}
return render(request,"main/auditform.html", context)
这将匹配回来 <a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>
或
尝试不通过主键而是通过 ID 本身检索对象...
path('AuditForm/<int:id>/', views.auditFormPage, name='AuditForm'),
views.py
def auditFormPage(request, id):
data= datas.objects.get(id=id)
context = {'data': data}
return render(request,"main/auditform.html", context)
内html
`<a class="dropdown-items" href ="{% url 'AuditForm' item.id %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>`
这应该会带您到页面。
如果有人问这个问题,我很抱歉,但我找不到任何可以帮助我解决小问题的东西。
我已经设法创建了一个 django 框架,并创建了一个模型和一个包含来自 csv 文件的数据的表单。
基本上我想做的是:
我创建了一个“主页”,看起来像这样(它只是理解这个想法的基础 运行):
假设我创建了一个 form.html 文件,并且 ID 喜欢它以显示 form/model 中与我单击的那个 ID 相关的所有信息。
可能跟Href有关link?
如果我不太清楚,我深表歉意,但请随时问我任何问题,我会尽力提供帮助 这些是我的文件:
Model.py:
class datas(models.Model):
country = models.CharField(_('country'),max_length=200,default='Null')
qs_login = models.CharField(_('qs_login'),max_length=200,default='Null')
Status = models.CharField(_('Status'),max_length=200,default='Null')
seller_id = models.CharField(_('seller_id'),max_length=200,default='Null')
Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True)
associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null')
associate = models.CharField(_('associate'),max_length=200,default='Null')
metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null')
associate_action = models.CharField(_('associate_action'),max_length=200,default='Null')
correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null')
Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null')
Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null')
Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null')
QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null')
Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null')
investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null')
investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null')
investigator = models.CharField(_('investigator'),max_length=200,default='Null')
verification_action = models.CharField(_('verification_action'),max_length=200,default='Null')
correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null')
Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null')
Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null')
QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null')
General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null')
Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null')
def __str__(self):
return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"
#def __str__(self):
# return f"{self.country}-{self.qs_login}"
# super().save(*args,**kwargs)
Views.py:
def homePage(request):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(request,"main/home.html",context)
def auditFormPage(response):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(response,"main/auditform.html", context)
这是列表所在的 Home.html(如你所见,我将 item.Task_ID 和 item.qs_login:
{% for item in items %}
<div class="container-fluid">
<li class="nav-item dropdown">
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-items" href="#">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
</ul>
</li>
</div>
{% endfor %}
更新:
我尝试做的是在 views.py 页面中创建一个函数:
def auditFormPage(request,pk):
model = datas.objects.filter(qs_login='nicobari')
context = {'items': model }
return render(request,"main/auditform.html", context)
然后我在 urls.py 文件中创建了一个路径,该文件采用动态 link 和主键:
path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),
并且在 dashboard/home 中我添加了一个 href link 到动态页面:
{% for item in items %}
<div class="container-fluid">
<li class="nav-item dropdown">
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
</ul>
</li>
</div>
{% endfor %}
但后来我收到 404 错误,因为找不到页面(我猜它正在寻找 AuditForm/str:pk/ 但只找到 AuditForm/):
Models.py:
class datas(models.Model):
country = models.CharField(_('country'),max_length=200,default='Null')
qs_login = models.CharField(_('qs_login'),max_length=200,default='Null')
Status = models.CharField(_('Status'),max_length=200,default='Null')
seller_id = models.CharField(_('seller_id'),max_length=200,default='Null')
Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True)
associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null')
associate = models.CharField(_('associate'),max_length=200,default='Null')
metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null')
associate_action = models.CharField(_('associate_action'),max_length=200,default='Null')
correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null')
Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null')
Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null')
Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null')
QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null')
Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null')
investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null')
investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null')
investigator = models.CharField(_('investigator'),max_length=200,default='Null')
verification_action = models.CharField(_('verification_action'),max_length=200,default='Null')
correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null')
Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null')
Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null')
QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null')
General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null')
Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null')
def __str__(self):
return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"
谢谢
更新答案
因为您 Task_ID
已设置 primary_key=True
。
path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),
为了使用传递给 auditFormPage
视图的 pk
从模型列表中检索特定对象,您可以在模型列表上使用 get
方法。
def auditFormPage(request, pk):
data= datas.objects.get(Task_ID=pk)
context = {'data': data}
return render(request,"main/auditform.html", context)
这将匹配回来 <a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>
或
尝试不通过主键而是通过 ID 本身检索对象...
path('AuditForm/<int:id>/', views.auditFormPage, name='AuditForm'),
views.py
def auditFormPage(request, id):
data= datas.objects.get(id=id)
context = {'data': data}
return render(request,"main/auditform.html", context)
内html
`<a class="dropdown-items" href ="{% url 'AuditForm' item.id %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>`
这应该会带您到页面。