我无法访问“产品”应用的详细信息

时间:2019-02-18 13:47:08

标签: python django python-3.x

我在Django项目中有两个应用程序:productsblog。 其他所有内容均正常运行,但在我的产品列表中:/products/产品标题所在的位置,当我单击标题时,我停留在同一页面上:/products。但是,当我直接进入products/1products/2时,它可以工作。

product_list.html:

{% extends 'base.html' %}

{% block content %}

{% for instance in object_list %}
<p>{{ instance.id }} -  <a href="{{ instance.get_absolute_url }}"> {{ 
instance.title }}</a> </p>

{% endfor %}
{% endblock  %}      

models.py:

from django.db import models
from django.urls import reverse

class Product(models.Model) :
    title       = models.CharField(max_length=130)
    description = models.TextField(blank=True,null=True)
    price       = models.DecimalField(max_digits=5,decimal_places=2)
    summary     = models.TextField()
    Featured    = models.BooleanField(default=False)


    def get_absolute_url(self,id):
       return  reverse("products : product_detail",kwargs={"id":self.id})

网址:

from django.contrib import admin
from django.urls import path

from products.views import (
  product_detail_view ,
  product_create_view ,
  product_delete_view ,
  product_list_view ,
  product_update_view ,
)
app_name = 'products'

urlpatterns = [


path('<int:id>/', product_detail_view, name='product_detail'),
path('create/', product_create_view, name='product_create'),
path('<int:id>/delete/', product_delete_view, name='product_delete'),
path('', product_list_view, name='product_list'),
path('<int:id>/update/', product_update_view, name='product_update'),


]

观看次数:

from django.shortcuts import render, get_object_or_404 , redirect
from .models import Product
from .forms import ProductForm

def product_detail_view(request,id):
     obj=get_object_or_404(Product,id=id)
     context = {
        'object' : obj
    }
    return render( request , "products/product_detail.html" , context )

def product_create_view(request):
    form=ProductForm(request.POST or None)
    if form.is_valid():
        form.save()
        form = ProductForm()
    context = {
        'form' : form
    }
        return render( request , "products/product_create.html" , context )


def product_delete_view(request,id) :
    obj=get_object_or_404(Product,id=id)
    if request.method == "POST":
        obj.delete()
        return redirect("../../")
    context = {
        'object': obj

    }
    return render( request , "products/product_delete.html" , context )

def product_list_view(request):
    queryset = Product.objects.all()   #list of objects
    context = {
    'object_list' : queryset
    }
     return render( request , "products/product_list.html" , context )

def product_update_view(request,id=id):
    obj=get_object_or_404(Product,id=id)
    form = ProductForm(request.POST or None,instance=obj)
    if form.is_valid():
        form.save()
    context = {
        'form': form
    }
    return render(request, "products/product_create.html", context)

有帮助吗?

1 个答案:

答案 0 :(得分:0)

根据@shekhar删除函数id的{​​{1}}参数后

在该行中,

get_absolute_url()

尝试删除return reverse("products : product_detail",kwargs={"id":self.id})前面的空格。 product_detail的空格前面使它在product_detail的前面char处成为一个{strong>不同值,同时为函数char p设置了参数

reverse