【问题标题】:Django NoReverseMatch Reverse for DetailView详细视图的 Django NoReverseMatch 反向
【发布时间】:2013-10-22 10:24:35
【问题描述】:

当我重定向回 Django 1.5 中的详细视图时,我的应用程序抛出错误。

NoReverseMatch:使用参数反转“InventoryPlotDetailView” 未找到“(3,)”和关键字参数“{}”。

它获得了正确的 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)

【问题讨论】:

  • 您是否尝试过使用名称“plot_detail”作为反转的第一个参数?
  • 我注意到的另一件事是 pk 是一个命名参数,因此您需要尝试传递 kwargs 而不是 args 来反转。
  • 感谢您的回复,内森。我通过了 kwargs 并将参数更改为 plot_detail,以及我可以轻松想到的每个组合,但仍然出现错误:Reverse for 'plot_detail' with arguments '()' and keyword arguments '{'pk': 3}'没有找到。
  • 我刚刚为有问题的两个表添加了模型定义。如果信息有用,库存图和库存数据之间存在多对一关系。
  • 您是否对视图进行了命名?发布完整的urls.py

标签: python django detailview


【解决方案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})

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 2015-02-13
    • 2020-10-30
    • 2017-04-10
    • 2020-08-23
    • 1970-01-01
    • 2014-01-20
    • 2023-03-21
    • 2020-12-24
    相关资源
    最近更新 更多