我是Django的新手,使用的是2.0版本。根据“Django by Example”一书开始构建“商店”应用程序。但是这本书使用的是django 1.8版本。 问题是,当我浏览http://127.0.0.1:8000/时会出现此错误:
“NoReverseMatch at / 使用参数'(None,'alpina')'找不到'product_detail'的反转。尝试了1种模式:['(?P \ d +)/(?P [ - \ w] +)/ $']“。
'alpina'是我从管理站点添加的产品名称。
以下是我的应用的urls.py:
from django.conf.urls import url
from . import views
app_name='shop'
urlpatterns = [
url(r'^$', views.product_list, name='product_list'),
url(r'^(?P<category_slug>[-\w]+)/$',
views.product_list,
name='product_list_by_category'),
url(r'^(?P<id>\d+)/(?P<slug>[-\w]+)/$',
views.product_detail,
name='product_detail'),
]
以下是我的应用的models.py:
from django.urls import reverse
from django.db import models
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',
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 )
stock = models.PositiveIntegerField()
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 __init__(self, *args, **kwargs):
super().__init__( *args, **kwargs )
self.id = None
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse( 'shop:product_detail',
args=[self.id, self.slug] )
以下是我的项目的views.py:
from django.shortcuts import render, get_object_or_404
from .models import Category, Product
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request,
'shop/product/list.html',
{'category': category,
'categories': categories,
'products': products})
def product_detail(request, id, slug):
product = get_object_or_404(Product,
id=id,
slug=slug,
available=True)
return render(request,
'shop/product/detail.html',
{'product': product})
这是我的项目的urls.py:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('shop.urls', namespace='shop')),
]
答案 0 :(得分:4)
从产品型号中删除
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, CustomDate.date);// your date object.value
calendar.set(Calendar.YEAR, CustomDate.year); //your date object.value
calendar.set(Calendar.MONTH, CustomDate.month); //your date object.value
calendar.set(Calendar.HOUR_OF_DAY, CustomTime.hour); //your time object.value
calendar.set(Calendar.MINUTE, CustomTime.minute);//your time object.value
calendar.set(Calendar.SECOND, CustomTime.second);//your time object.value
System.out.println(calendar.getTime());