【发布时间】:2020-01-22 04:15:37
【问题描述】:
我正在制作一本食谱书。出于某种原因,每当我尝试从数据库中提取食谱进行编辑时,我都会收到一个错误,即找不到我指定的食谱。我正在使用蛞蝓,我的逻辑是我将从已经提取数据库信息的详细视图转到更新视图。我正在尝试将已经从 detailView 中提取的配方对象传递给 updateView,但是当我这样做时,它一直告诉我找不到指定的配方。
views.py: 我在这里调用的基本视图只是提供了一个默认的 post 方法来处理搜索,这样我就不必为我创建的每个视图都放入它,所以我有一些代码可重用性 p>
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'),
recipe_detail.html 中的链接:
<a href="{% url 'RecipeBook:edit_recipe' recipe.slug %}" style="float: right">Edit Recipe</a>
我一直在发疯试图弄清楚。根据我在这里的所有内容,我在 detailView 中提取的配方应该能够传递给 editView,但是每次我尝试打开 edit_recipe 页面时,它总是告诉我它不能找到指定的配方。它生成的 URL 显示了它应该显示的正确的 slug 和链接。我不知道此时我错过了什么......
【问题讨论】:
-
使用 self.get_object() 代替 self.object
-
这没有任何区别。我之前在其他项目中也使用过 self.object 并且效果很好。我还要注意,我尝试创建一个单独的变量来从数据库执行额外查询以将配方传递给 edit_recipe url,但这也不起作用。
标签: python django django-urls