python - Django编辑模型实例找不到实例
问题描述
我正在制作食谱书。出于某种原因,每当我尝试从数据库中提取食谱进行编辑时,我都会收到一个错误,即找不到我指定的食谱。我正在使用蛞蝓,我的逻辑是我将从已经提取数据库信息的详细视图转到更新视图。我试图将我已经从 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
模型.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
网址.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'),
recipe_detail.html 中的链接:
<a href="{% url 'RecipeBook:edit_recipe' recipe.slug %}" style="float: right">Edit Recipe</a>
我一直在发疯,试图弄清楚。根据我在这里的所有内容,我在 detailView 中提取的配方应该能够传递给 editView,但是每次我尝试打开 edit_recipe 页面时,它总是告诉我它不能找到指定的配方。它生成的 URL 显示了它应该显示的正确的 slug 和链接。我不知道此时我错过了什么......
解决方案
我最终不得不返回并将视图更改为 DetailView。这是我可以推动配方实例的唯一方法。使用更新视图与不是很清楚的模型有一些非常具体的事情......
一旦我更改为 DetailView,页面将填充初始化为配方值的表单。然后我可以进行调整以确保一切正常。
感谢那些做出回应的人,它至少让我的大脑朝着不同的方向工作以解决这个问题。
推荐阅读
- android - Android Studio 没有正确显示布局
- python - 计算 TSV 文件中所有其他点之间的距离?
- javascript - 将 object.constructor 与其构造函数和 instanceof 进行比较有什么区别?
- c++11 - 在 ARM 计算库中初始化张量的正确方法?
- c# - 如何在字符串末尾包含名称的特定部分
- autocomplete - Vue2自动完成组件不保留输入值
- java - 'for' 结构:char 的使用
- angular - 对话框弹出未填充 Angular Material ts
- ios - 从 Apple News API 检索头条新闻
- d3.js - 向路径添加点