Django:提交表单时从FormView重定向到详细信息。
相关表单视图:
class addrecipe(FormView):
form_class = AddRecipeForm
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}
相关表格:
class AddRecipeForm(forms.ModelForm):
name = forms.CharField(max_length="50", label="Recipe Name")
description = forms.Textarea(attrs={'class': 'desc-text-area'})
servings = forms.IntegerField()
tools = forms.ModelMultipleChoiceField(queryset=Tool.objects.all(), widget=forms.CheckboxSelectMultiple, required = True, help_text="Select all relevant tools")
class Meta:
model = Recipe
fields = ("__all__")
详细信息的URL模式查看页面:
path('<int:pk>/recipedetails', views.recipedetails.as_view(), name='recipe_details'),
我想让用户提交表单,然后将其列入数据库中的条目的详细信息页面。 i' VE尝试使用REVERSE/REVERSE_LAZY使用成功URL执行此操作,但这还没有成功。
我还尝试在我的表单视图类中添加以下内容:
def get_success_url(self):
test_recipe_id = self.object.id
return reverse('recipeBook:recipe_details', pk=test_recipe_id)
在更改我的路径之后:
re_path(r'(?P<pk>[^/]+)/recipedetails', views.recipedetails.as_view(), name='recipe_details'),
我会收到以下值错误:
AttributeError at /recipebook/addrecipe
'addrecipe' object has no attribute 'object'
Relevant FormView:
class addrecipe(FormView):
form_class = AddRecipeForm
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}
Relevant Form:
class AddRecipeForm(forms.ModelForm):
name = forms.CharField(max_length="50", label="Recipe Name")
description = forms.Textarea(attrs={'class': 'desc-text-area'})
servings = forms.IntegerField()
tools = forms.ModelMultipleChoiceField(queryset=Tool.objects.all(), widget=forms.CheckboxSelectMultiple, required = True, help_text="Select all relevant tools")
class Meta:
model = Recipe
fields = ("__all__")
URL pattern for the details view page:
path('<int:pk>/recipedetails', views.recipedetails.as_view(), name='recipe_details'),
I want to have the user submit the form, then be taken to the details page of the entry they just made into the database. I've tried doing this using reverse/reverse_lazy with a success url but that hasn't been successful.
I also tried adding the following to my form view class:
def get_success_url(self):
test_recipe_id = self.object.id
return reverse('recipeBook:recipe_details', pk=test_recipe_id)
After also changing my path to:
re_path(r'(?P<pk>[^/]+)/recipedetails', views.recipedetails.as_view(), name='recipe_details'),
I get the following Value error:
AttributeError at /recipebook/addrecipe
'addrecipe' object has no attribute 'object'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的解决方案几乎在那里。
您可以使用
get_success_url
方法在模型之后获取食谱ID。这将允许您重定向参数。您的
详细信息URL
未按预期接收参数旧:
new:
Your solution was almost there.
You could use the
get_success_url
method to get the recipe ID after the model. This will allow you redirect with parameters.Your
detail url
is not receiving the parameter as expected hence it needs to be reconfigured with a new regexOld:
New:
我需要使用httpresponsedirect正确重定向。我的视图最终看起来像这样:
在抓取ID之前保存对象似乎是一个必要的步骤,因为我发现仅在创建对象时创建ID本身。
相反的回报不起作用,所以老实说,我在前面称呼玛丽的httpresponsedilect,它起作用了。如果我弄清楚原因,我将更新答案。
I needed to use HttpResponseRedirect to redirect correctly. My view ended up looking like this:
Saving the object before grabbing the ID appears to be a necessary step as I found that the ID itself is only created when the object is created.
The reverse return wasn't working, so honestly I hail mary'd a httpresponseredirect in front and it worked. I will update the answer if I figure out why..