我使用了Django-groundwork这个第三方app
django 1.6 python 2.7
输入http://localhost:8000/depotapp/product/list/后出现错误
In template /home/tml/PycharmProjects/depot/templates/depotapp/list_product.html, error at line 22
Reverse for 'depotapp.views.view_product item.id' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
depot/tempates/depot/list_product.html
{% extends "base.html" %}
{%block title%}产品清单{% endblock %}
{% block content %}
<div class="container">
<div class="container">
</div>
</div>
{% for item in list_items.object_list %}
<div class="row" style="padding-top:2px">
<div class="span3 media-grid">
<a href="#">
<img class="thumbnail" src="{{item.image_url}}" alt="">
</a>
</div>
<div class="span-two-thirds">
<h4>{{item.title}}</h4>
{{item.description}}
</div>
<div class="span2" style="align:righ">
<p><a class="btn primary" href="{% url 'depotapp.views.view_product item.id' %}">查看</a> </p>
<p><a class="btn success" href="{% url 'depotapp.views.edit_product item.id' %}">编辑</a> </p>
<p><a class="btn danger" href="#">删除</a></p>
{% endfor %}
{% if list_items.has_previous %}
<a href="?page={{ list_items.previous_page_number }}">上一页</a>
{% endif %}
<span class="current">
第{{ list_items.number }}页,共{{ list_items.paginator.num_pages }}页
</span>
{% if list_items.has_next %}
<a href="?page={{ list_items.next_page_number }}">下一页</a>
{% endif %}
<p>
<a href="{% url "depotapp.views.create_product" %}">新增产品</a>
</p>
{% endblock %}
depot/depot/urls.py
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'depot.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('',
url(r'^depotapp/', include('depotapp.urls')),
)
urlpatterns += staticfiles_urlpatterns()
depot/depotapp/urls.py
from django.conf.urls import
from models import
from views import *
urlpatterns = patterns('',
(r'product/create/$', create_product),
(r'product/list/$', list_product ),
(r'product/edit/(?P<id>[^/]+)/$', edit_product),
(r'product/view/(?P<id>[^/]+)/$', view_product),
)
depot/depotapp/views.py
from django import forms
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import get_template
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
from models import *
from forms import *
def create_product(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()
form = ProductForm()
t = get_template('depotapp/create_product.html')
c = RequestContext(request,locals())
return HttpResponse(t.render(c))
def list_product(request):
list_items = Product.objects.all()
paginator = Paginator(list_items ,10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
list_items = paginator.page(page)
except :
list_items = paginator.page(paginator.num_pages)
t = get_template('depotapp/list_product.html')
c = RequestContext(request,locals())
return HttpResponse(t.render(c))
def view_product(request, id):
product_instance = Product.objects.get(id = id)
t=get_template('depotapp/view_product.html')
c=RequestContext(request,locals())
return HttpResponse(t.render(c))
def edit_product(request, id):
product_instance = Product.objects.get(id=id)
form = ProductForm(request.POST or None, instance = product_instance)
if form.is_valid():
form.save()
t=get_template('depotapp/edit_product.html')
c=RequestContext(request,locals())
return HttpResponse(t.render(c))