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

时间:2017-06-24 09:09:07

标签: python django

我在浏览器中收到此错误

 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.

我基本上是django的初学者并创建我的django商店应用程序。通过输入网址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)

这是我的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

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

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

我试着检查一切,但一切看起来都不错

3 个答案:

答案 0 :(得分:1)

问题在于您的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)

答案 1 :(得分:1)

使用这些命令来迁移文件:

python manage.py makemigrations
python manage.py migrate

答案 2 :(得分:-2)

prepopulated_fields *** =[' ***蛞蝓 ':(' 姓名”,)}

prepopulated_fields = { '蛞蝓' :( '姓名',)}

[eror {