Django 编辑模型实例找不到实例

Django editing a model instance fails to find the instance

我正在制作食谱书。出于某种原因,每当我尝试从数据库中提取食谱进行编辑时,我总是收到错误消息,找不到我指定的食谱。我正在使用 slugs,我的逻辑是我要从已经提取数据库信息的 detailView 转到 updateView。我试图将我已经从 detailView 中提取的食谱对象传递给 updateView,但是当我这样做时,它一直告诉我找不到指定的食谱。

views.py: 我在这里调用的基本视图只提供默认的 post 方法来处理搜索,这样我就不必为我创建的每个视图都放入它,所以我有一些代码可重用性

class RecipeDetailView(BaseDetailView):
    model = Recipe
    template_name = 'RecipeBook/recipe_detail.html'
    context_object_name = 'recipe_view'
    queryset = None
    slug_field = 'slug'
    slug_url_kwarg = 'slug'

    def get_context_data(self, *args, **kwargs):
        context = super(RecipeDetailView, self).get_context_data()
        recipe = self.object
        recipe.ingredients = recipe.ingredients_list.split('\n')

        context['recipe'] = recipe

        return context

class RecipeEditView(BaseUpdateView):
    model = Recipe
    template_name = 'RecipeBook/edit_recipe.html'
    context_object_name = 'recipe_edit'
    queryset = None
    slug_field = 'slug'
    slug_url_kwarg = 'slug'
    form_class = RecipeForm

    def get_context_data(self, *args, **kwargs):
        context = super(RecipeEditView, self).get_context_data()
        recipe = self.object
        print(recipe.name)
        recipe.ingredients = recipe.ingredients_list.split('\n')
        recipe.categories_list = ""
        categories = Category.objects.filter(recipe=recipe)
        for category in categories:
            if category != categories[-1]:
                recipe.categories_list += (category + ", ")
            else:
                recipe.categories_list += category

        recipe_edit_form = RecipeForm(initial={'name': recipe.name, 'ingredients_list': recipe.ingredients,
                                               'directions': recipe.directions, 'prep_time': recipe.prep_time,
                                               'cook_time': recipe.cook_time, 'servings': recipe.servings,
                                               'source': recipe.source, 'category_input': recipe.categories_list})
        context['recipe'] = recipe
        context['recipe_edit_form'] = recipe_edit_form

        return context

models.py:

class Recipe(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=100, default="")
    ingredients_list = models.TextField(default="")
    servings = models.IntegerField(default=0, null=True, blank=True)
    prep_time = models.IntegerField(default=0, null=True, blank=True)
    cook_time = models.IntegerField(default=0, null=True, blank=True)
    directions = models.TextField(default="")
    source = models.CharField(max_length=100, default="", null=True, blank=True)
    categories = models.ManyToManyField(Category, blank=True)
    slug = models.CharField(max_length=200, default="")

    def __str__(self):
        return self.name

urls.py

# ex: /Recipes/Grilled_Chicken/
    path('Recipes/<slug>/', views.RecipeDetailView.as_view(), name='view_recipe'),
    path('Recipes/<path:slug>/', views.RecipeDetailView.as_view(), name='view_recipe'),
    # ex: /Recipes/edit/Steak/
    path('Recipes/edit/<slug>/', views.RecipeEditView.as_view(), name='edit_recipe'),
    path('Recipes/edit/<path:slug>/', views.RecipeEditView.as_view(), name='edit_recipe'),

link 在 recipe_detail.html:

<a href="{% url 'RecipeBook:edit_recipe' recipe.slug %}" style="float: right">Edit Recipe</a>

我一直在疯狂地想弄明白。根据我在这里的所有内容,我在 detailView 中提取的食谱应该能够传递给 editView,但每次我尝试打开 edit_recipe 页面时,它都会告诉我它找不到指定的食谱。它生成的 URL 显示了正确的 slug 和它应该显示的 link 。我不知道我现在错过了什么...

试试这个方法:

<a href="{% url 'RecipeBook:edit_recipe' recipe_view.slug %}" style="float: right">Edit Recipe</a>

我最终不得不返回并将视图更改为 DetailView。这是我可以让食谱实例被推送的唯一方法。对于不太清楚的模型使用更新视图有一些非常具体的事情......

一旦我更改为 DetailView,页面将填充初始化为配方值的表单。然后我可以进行调整以确保一切正常。

感谢那些回复的人,它至少让我的大脑转向了不同的方向来解决这个问题。