2016-08-12 82 views
0

here is screenshot error 我不明白爲什麼我在點擊詳細信息頁面中的添加照片按鈕後出現錯誤,但如果我鍵入url它工作正常,但提交後我想返回到詳細信息頁面,但它給了我這個錯誤 我views.pypython webframework django noreversematch

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'] 

我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'), 

]

我的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 
+2

的[什麼是NoReverseMatc可能的複製H誤差,如何解決呢?(http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5

+0

是這些空間實際存在於你的網址?只是想知道 –

+0

顯示模板,你顯示按鈕的部分 – pleasedontbelong

回答

1

問題是你沒有通過模板中的pk。從截圖,如果你看一下在模板高亮線很明顯,你的網址卻丟失了所需要的關鍵字參數...

同樣是在錯誤頁面的第一行還提到(與關鍵字ARGS {})

只要提到它的網址名稱後,它應該工作

{% url 'gallery:add-photo' photo.pk %} 

,或者您也可以做

{{ photo.get_absolute_url }}