【问题标题】:DJANGO error. Reverse for 'receipt' with arguments '('',)' not found. 1 pattern(s) tried: ['receipt/(?P<pk>[^/]+)/$'] trying to use ID:PKDJANGO 错误。未找到带有参数 '('',)' 的 'receipt' 的反向操作。尝试了 1 种模式:['receipt/(?P<pk>[^/]+)/$'] 尝试使用 ID:PK
【发布时间】:2021-11-09 04:51:38
【问题描述】:

这里发生了很多事情,inventory.Html 中的最后一行假设使用views.py 中产品的ID 链接到产品页面。我正在尝试使用 ID 为每个产品创建一个页面,但路径不起作用。

inventory.html 代码

                    {% for i in items %}
                    
                
                        <tr>
                            <td>{{i.Name_of_the_Material_per_specification}}</td>
                            <td>{{i.Site_Material_Code }}</td>
                            <td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>
                            
                            
                        </tr>
                        
                    
                    {% endfor %}
                    

RECIEPT.HTML

    <div class="col-md">
            <div class="card card-body">
                        {% csrf_token %}
                        {% for i in products %}

                        <p>Name: {{products.Name_of_the_Material_per_specification}}</p>
                        
                        
                        {% endfor %}
        
            </div>
        </div>  

URLS.PY

from django.contrib import admin
from django.urls import path, include
from inventory import views


urlpatterns = [
    path('main', views.inventory, name='inventory'),
    path('receipt/<str:pk>/', views.products, name="receipt")

views.py

from django.shortcuts import render, HttpResponse

from django.template import loader
from django.shortcuts import redirect
from .models import *
from .forms import *



# Create your views here.
def inventory(request):
    items = materialForm.objects.all()
  
    return render(request, 'website/inventory.html', {'items': items})

def products(request, pk):
    products = materialForm.objects.get(id=pk)
    context = {'products':products }

    return render(request, 'website/receipt.html', context )

models.py

class materialForm(models.Model):

    Name_of_the_Material_per_specification = models.CharField(max_length=100, null=True)

    def __str__(self):
        return self.Name_of_the_Material_per_specification

【问题讨论】:

  • 您正在使用 i 进行迭代,但将 products.id 作为参数发送...再次检查
  • 我可以同时迭代吗?
  • 请附上你的模型

标签: python html django


【解决方案1】:

你正在这样做:

<td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>

但是,products.id 为空或不存在,所以这可能是您想要的:

<td><a class="btn btn-sm btn-info" href="{% url 'receipt' i.id %}">View</a></td>

更新

出现新错误是因为您尝试迭代单个对象。所以,删除{% for i in products %}{% endfor %}

我还建议更改 products 的名称,因为它会造成混淆并可能导致错误。

【讨论】:

  • 修复了一些问题。但现在我收到“'materialForm' 对象不可迭代”。我使产品功能可以从每一行获取特定的 ID。我在数据库中有三个对象,我希望使用它们的 ID 链接一个页面,给出它们的特定对象信息。
  • @NickRizzolo,我刚刚更新了我的答案。
【解决方案2】:

你得到 [ 但现在我收到“'materialForm' 对象不可迭代”。 ] 因为在 receipts.html 中,您正在使用 i 迭代产品查询集中的内容,但是在循环中您再次直接使用产品查询集的名称而不是使用创建的迭代器 i...


进行inventory.html和receipt.html中提到的更改,然后重试..,

inventory.html 代码

                {% for i in items %}
                
            
                    <tr>
                        <td>{{i.Name_of_the_Material_per_specification}}</td>
                        <td>{{i.Site_Material_Code }}</td>
                        #change the next line to
                        <td><a class="btn btn-sm btn-info" href="{% url 'receipt' products.id %}">View</a></td>

                        #this
                        <td><a class="btn btn-sm btn-info" href="{% url 'receipt' i.id %}">View</a></td>
                        
                        
                    </tr>
                    
                
                {% endfor %}
                

RECIEPT.HTML

<div class="col-md">
        <div class="card card-body">
                    {% csrf_token %}
                    {% for i in products %}
                    #change the next line to
                    <p>Name: {{products.Name_of_the_Material_per_specification}}</p>
                    #this
                    <p>Name: {{i.Name_of_the_Material_per_specification}}</p>
                    
                    {% endfor %}
    
        </div>
    </div>  

URLS.PY

from django.contrib import admin
from django.urls import path, include
from inventory import views


urlpatterns = [
    path('main', views.inventory, name='inventory'),
    path('receipt/<str:pk>/', views.products, name="receipt")

views.py

from django.shortcuts import render, HttpResponse

from django.template import loader
from django.shortcuts import redirect
from .models import *
from .forms import *



# Create your views here.
def inventory(request):
    items = materialForm.objects.all()
  
    return render(request, 'website/inventory.html', {'items': items})

def products(request, pk):
    products = materialForm.objects.get(id=pk)
    context = {'products':products }

    return render(request, 'website/receipt.html', context )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-12
    • 2022-01-13
    • 2020-11-24
    • 2021-06-13
    • 2020-02-06
    • 2022-01-17
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多