找不到页面(404)更新产品

问题描述 投票:0回答:1

我不知道我的设置有什么问题我的网址.py

from django.urls import path
from . import views

app_name = 'shop'

urlpatterns = [
    path('', views.product_list, name='product_list'),

    path('<slug:category_slug>/', views.product_list, name='product_list_by_category'),

    path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),

    path('shop/Create_Product/', views.Create_Product, name='Create_Product'),

    path('shop/product/Edit_Product/', views.Edit_Product, name='Edit_Product'),        

]

Forms.py

from django import forms
from django.contrib.auth.models import User
from .models import Product

class CreateProduct(forms.ModelForm):

    class Meta:
        model = Product
        fields = ["category", "name", "image", "description",
                  "price", "available", 'slug']
        prepopulated_fields = {'slug': ('name',)}

class EditProduct(forms.ModelForm):

    class Meta:
        model = Product
        fields = ["category", "name", "image", "description",
                  "price", "available"]
        prepopulated_fields = {'slug': ('name',)}

Models.py

class Product(models.Model):
    category = models.ForeignKey(Category,
                                 related_name='products',
                                 on_delete=models.CASCADE)
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    image = models.ImageField(upload_to='products/%Y/%m/%d',
                              blank=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
            return reverse('shop:product_detail', args=[self.id, self.slug])

Views.py

@staff_member_required
def Edit_Product(request, id=None):
    instance = get_object_or_404(Product, id=id)
    if request.method == "POST":
        form = EditProduct(request.POST, instance=instance)
        if form.is_valid():
            product = form.save(commit=False)
            product.save()
            return redirect('shop/product/detail.html')
    else:
        form = EditProduct(instance=instance)
    return render(request,'shop/product/Edit_Product.html', {'form': form, 'product': instance})

[任何人都可以提供帮助,我是django的一个真正的菜鸟,我看了互联网,没有答案,我试图更改url模式,但也没有希望,有时它显示缺少关键的位置参数,有时出现404错误

python django web post get
1个回答
0
投票
def Edit_Product(request, id=None):
    instance = get_object_or_404(Product, id=id)

在这种情况下,您的网址必须是这样的:

path('shop/product/Edit_Product/<int:id>', views.Edit_Product, name='Edit_Product'),

而不是:

path('shop/product/Edit_Product/', views.Edit_Product, name='Edit_Product'),

传递您要编辑的产品的ID。

© www.soinside.com 2019 - 2024. All rights reserved.