简体   繁体   English

无法在管理站点中访问和添加产品?-Django

[英]not able to access and add product in admin site?-Django

i get this error in the browser我在浏览器中收到此错误

 Using the URLconf defined in myshop.urls, Django tried these URL patterns, in this order:
       ^admin/ ^$ [name='index']
            ^admin/ ^login/$ [name='login']
            ^admin/ ^logout/$ [name='logout']
            ^admin/ ^password_change/$ [name='password_change']
            ^admin/ ^password_change/done/$ [name='password_change_done']
            ^admin/ ^jsi18n/$ [name='jsi18n']
            ^admin/ ^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$ [name='view_on_site']
            ^admin/ ^auth/group/
            ^admin/ ^auth/user/
            ^admin/ ^sites/site/
            ^admin/ ^(?P<app_label>auth|sites)/$ [name='app_list']
        The current path, admin/myshop/Product/add/, didn't match any of these.

i am basically a beginner to django and creating my django shop app.我基本上是 django 的初学者并创建了我的 django 商店应用程序。 i get this error by typing the url http://127.0.0.1:8000/admin/myshop/Product/add/ My admin.py looks like this我通过输入 url http://127.0.0.1:8000/admin/myshop/Product/add/得到这个错误 我的 admin.py 看起来像这样

from django.contrib import admin
from .models import Category, Product

class CategoryAdmin(admin.ModelAdmin):
list_display=['name','slug']
prepopulated_fields={'slug':('name',)}
admin.site.register(Category, CategoryAdmin)

class ProductAdmin(admin.modelAdmin):
list_display=['name','slug','category','price','stock','avialable','created','updated']
list_filter=['available','created','updated','category']
list_editable=['price','stock','available']
prepopulated_fields=['slug':('name',)}
admin.site.register(Product, ProductAdmin)

This is my models.py file这是我的models.py文件

from django.db import models

# Create your models here.
from django.core.urlresolvers import reverse


class Category(models.Model):
name = models.Charfield(max_length=200, db_index=True)
slug = models.Slugfield(max_length=200, db_index=True, unique=True)

class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'

def__str__(self):
return self.name

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

class product(models.Model):
category=models.ForeignKey(Category, related_name='products')
name=models.CharField(max_length=200, db_index=True)
slug=models.SlugField(max_length=200, db_index=True)
image=models.ImageField(upload_to'product/%Y/%m/%d',blank=True)
description=models.TextField(blank=True)
price=models.DecimalField(max_digits=10, decimal_places=2)
stock=models.PositiveIntegerField()
available=models.BooleanField(default=True)
created=models.Datetimefield(auto_now_add=True)
updated=models.DatetimeField(auto_now=True)

class Meta:
ordering=('-created',)
index_togetther=(('id','slug'),)

def__str__(self):
return self.name

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

url.py网址.py

from django.conf.urls import url
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),

] ]

I tried to check everything but everything looks ok to me我试图检查一切,但对我来说一切都很好

The problem is with your admin.py file your model from the code you posted is having lowercase product class but in admin you took capital Product, so change the Product to product问题在于您的 admin.py 文件,您发布的代码中的模型具有小写的产品类,但在 admin 中您使用了资本产品,因此将产品更改为产品

from django.contrib import admin
from .models import Category, product

class CategoryAdmin(admin.ModelAdmin):
list_display=['name','slug']
prepopulated_fields={'slug':('name',)}
admin.site.register(Category, CategoryAdmin)

class ProductAdmin(admin.modelAdmin):
list_display=['name','slug','category','price','stock','avialable','created','updated']
list_filter=['available','created','updated','category']
list_editable=['price','stock','available']
prepopulated_fields=['slug':('name',)}
admin.site.register(product, ProductAdmin)

Use these commands for migrating files:使用以下命令迁移文件:

python manage.py makemigrations
python manage.py migrate

You need to use [ or { , that is:您需要使用[{ ,即:

prepopulated_fields***`=['`***slug':('name',)}

or

prepopulated_fields={'slug':('name',)}

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

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