2013-10-14 101 views
0

當我重定向回到Django 1.5中的詳細視圖時,我的應用程序發生錯誤。Django NoReverseMatch Reverse for DetailView

NoReverseMatch: Reverse for 'InventoryPlotDetailView' with arguments '(3,)' and keyword arguments '{}' not found.

它得到正確的forestinventoryplot_id,但似乎細節視圖不知道如何處理的說法。如果我手動訪問詳細視圖(http://[server]/geoapp/inventory_plot/detail/3/),它按預期工作。下面是相關的零碎片斷,有什麼建議?

Views.py:

class InventoryPlotDetailView(DetailView): 
    queryset = ForestInventoryPlot.objects.all() 
    template_name = 'geoapp/forestinventoryplot_detail.html' 
    context_object_name = 'plot_detail' 

def InventoryDataAdd(request, forestinventoryplot_id=1): 
    if request.method == 'POST': 
     form = InventoryDataForm(request.POST) 
     if form.is_valid(): 
      new_data = form.save()   
      return HttpResponseRedirect(reverse('geoapp:InventoryPlotDetailView', args=(new_data.forestinventoryplot_id,))) 
    else: 
     initial_data = {'forestinventoryplot' : forestinventoryplot_id} 
     form = InventoryDataForm(initial=initial_data)  
     return render(request, 'geoapp/forestinventorydata_add.html', {'form': form}) 

urls.py:

urlpatterns = patterns('', 
    url(r'^index$', views.Index), 
    url(r'^$', views.Index), 
    url(r'^inventory_plot/add/$', views.InventoryPlotAdd, name='inventory_plot_add'), 
    url(r'^inventory_plot/edit/(?P<forestinventoryplot_id>\d+)$', views.InventoryPlotEdit, name='inventory_plot_edit'), 
    url(r'^inventory_plot/delete/(?P<pk>\d+)$', views.InventoryPlotDelete, name='inventory_plot_delete'), 
    url(r'^map/$', views.map_page), 
    url(r'^map2/$', views.map2_page), 
    url(r'^inventory_plot/$', views.InventoryPlotListView.as_view(), name='inventory_plot_list'), 
    url(r'^inventory_plot/detail/(?P<pk>\d+)/$', views.InventoryPlotDetailView.as_view(), name='plot_detail'), 
    url(r'^inventory_data/add/$', views.InventoryDataAdd, name='inventory_data_add'), 
    url(r'^inventory_plot/(?P<forestinventoryplot_id>\d+)/add_data/$', views.InventoryDataAdd, name='inventory_data_add'), 
    url(r'^accounts/login/$', 'django.contrib.auth.views.login'), 
    url(r'^accounts/logout/$', views.logout_view), 
    url(r'^home/$', views.Home, name = 'home'), 
    url(r'^inventory_data/add/(?P<forestinventoryplot_id>\d+)/$', views.InventoryDataAdd, name='inventory_data_addition'), 
) 

Models.py:

class ForestInventoryPlot(models.Model): 
    forestinventoryplot_id = models.AutoField(primary_key=True) 
    plot_area_ft2 = models.DecimalField(null=True, blank=True, max_digits=5, decimal_places=1) 
    plot_radius_ft = models.DecimalField(max_digits=4, decimal_places=1, blank=True, null=True) 
    plot_length_x_ft = models.DecimalField(max_digits=4, decimal_places=1, blank=True, null=True) 
    plot_length_y_ft = models.DecimalField(max_digits=4, decimal_places=1, blank=True, null=True) 
    plot_geometry = models.CharField(max_length=30, null=True, blank=True) 
    geometry = models.PointField(srid=4326, null=True, blank=True) 
    elevation = models.IntegerField(null=True, blank=True) 
    position_description = models.CharField(max_length=255, null=True, blank=True) 
    plot_create_date = models.DateField(null=True, blank=True) 
    created_by = models.CharField(max_length = 100) 
    objects = models.GeoManager() 
    class Meta: 
      db_table = 'forest_inventory_plot' 
      ordering = ["forestinventoryplot_id"] 
    def __unicode__(self): 
     return unicode(self.forestinventoryplot_id) 

class ForestInventoryData(models.Model): 
    forestinventorydata_id = models.AutoField(primary_key=True) 
    forestinventoryplot = models.ForeignKey('ForestInventoryPlot', null=True, blank=True) 
    tree = models.ForeignKey('Tree', null=True, blank=True) 
    collection_date = models.DateField(null=True, blank=True) 
    species = models.CharField(max_length=30, null=True, blank=True) 
    dbh_in = models.DecimalField(max_digits=4, decimal_places=1, blank=True, null=True) 
    height_ft = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True) 
    class Meta: 
     db_table = 'forest_inventory_data' 
     ordering = ["forestinventorydata_id"] 
    def __unicode__(self): 
     return unicode(self.forestinventorydata_id) 
+2

您是否嘗試過使用名稱「plot_detail」作爲第一個參數來反向? –

+1

我注意到的另一件事是pk是一個命名參數,所以你會想嘗試傳遞kwargs而不是args來反轉。 –

+0

感謝您的迴應,內森。我傳遞了kwargs,並將參數改爲plot_detail以及我可以輕易想到的每個組合並仍然出現錯誤:使用參數'()'和關鍵字參數'{'pk':3}'反轉'plot_detail''未找到。 – akthor

回答

1

視圖需要使初級鍵爲kwarg,使用url.py條目(plot_detail)的名稱,並使用附加到它的名稱空間('geoapp:plot_detail')來使事情有效。

def InventoryDataAdd(request, forestinventoryplot_id=1): 
     if request.method == 'POST': 
      form = InventoryDataForm(request.POST) 
      if form.is_valid(): 
       new_data = form.save() 
       return HttpResponseRedirect(reverse('geoapp:plot_detail', kwargs={'pk':new_data.forestinventoryplot_id})) 
     else: 
      initial_data = {'forestinventoryplot' : forestinventoryplot_id} 
      form = InventoryDataForm(initial=initial_data) 
     return render(request, 'geoapp/forestinventorydata_add.html', {'form': form})