Django NoReverseMatch Reverse for DetailVi

2024-10-01 15:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

当我重定向回django1.5中的详细视图时,我的应用程序抛出了一个错误。在

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

它得到了正确的forestinventoryplot_id,但是detail视图似乎不知道如何处理这个参数。如果我手动访问detail视图(http://[server]/geoapp/inventory_plot/detail/3/),它将按预期工作。下面是相关的零碎,有什么建议吗?在

视图.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})

网址.py:

^{pr2}$

模型.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)

Tags: formidtrueplotmodelsnulllengthmax
1条回答
网友
1楼 · 发布于 2024-10-01 15:29:03

视图需要将主键作为kwarg传递,并使用网址.py条目(plot_detail),并使用附加到它的名称空间('geoapp:绘图详情'使事情运转起来。在

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})

相关问题 更多 >

    热门问题