here is screenshot error 我不明白为什么我点击添加照片按钮在详细信息页面后收到错误,但如果我键入网址它工作正常但提交后我想返回详细信息页面,但它给了我这个错误 我的views.py
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from .models import Category, Photo
class IndexView(generic.ListView):
template_name='gallery/index.html'
context_object_name='cat'
def get_queryset(self):
return Category.objects.all()
class DetailView(generic.DetailView):
model=Category
template_name='gallery/detail.html'
context_object_name='cater'
class CategoryCreate(CreateView):
model=Category
fields=['Category_title','artist','Category_logo']
my urls.py
from django.conf.urls import url
from . import views
app_name='gallery'
urlpatterns=[
url(r'^$',views.IndexView.as_view(),name='index'),
url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='detail'),
#adding photo to category
url(r'(?P<pk>[0-9]+)/add/$',views.PhotoCreate.as_view(),name='add- photo'),
url(r'category/add/$',views.CategoryCreate.as_view(),name='add-category'),
url(r'category/(?P<pk>[0- 9 ]+)/$',views.CategoryUpdate.as_view(),name='update-category'),
my models.py
from django.db import models
from django.core.urlresolvers import reverse
# Create your models here.
class Category(models.Model):
Category_title=models.CharField(max_length=200)
artist=models.CharField(max_length=200)
Category_logo=models.FileField()
# returns to details page after submitting category form
def get_absolute_url(self):
return reverse('gallery:detail',kwargs={'pk':self.pk})
def __str__(self):
return self.Category_title + '-' +self.artist
class Photo(models.Model):
cat=models.ForeignKey(Category,on_delete=models.CASCADE)
file_type=models.FileField()
photo_title=models.CharField(max_length=100)
# returns to details page after submitting photo form
def get_absolute_url(self):
return reverse('gallery:add-photo',kwargs={'pk':self.pk})
def __str__(self):
return self.photo_title
答案 0 :(得分:1)
问题是您没有在模板中传递pk
。从屏幕截图中,如果您查看模板中的突出显示行,很明显您的网址缺少所需的关键字参数...
在错误页面的第一行(使用关键字args {})
中也提到了相同的内容只需在网址名称后面提及它就可以了
{% url 'gallery:add-photo' photo.pk %}
或者您也可以
{{ photo.get_absolute_url }}