Django:提交表单时从FormView重定向到详细信息。

发布于 2025-01-30 08:11:25 字数 1477 浏览 2 评论 0原文

相关表单视图:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2025-02-06 08:11:25

您的解决方案几乎在那里。
您可以使用get_success_url方法在模型之后获取食谱ID。这将允许您重定向参数。

class addrecipe(FormView):
    form_class = AddRecipeForm
    model = Recipe
    template_name = 'recipebook/addrecipe.html'
    fields = '__all__'
    extra_context = {
        'recipe_list': Recipe.objects.all()
    }
    
    #New method
    def get_success_url(self):
        test_recipe_id = self.object.id #gets id from created object
        return reverse('recipeBook:recipe_details', pk=test_recipe_id)

您的详细信息URL未按预期接收参数
旧:

path('<int:pk>/recipedetails', views.recipedetails.as_view(), name='recipe_details'),

new:

from django.urls import path, re_path

re_path(r'(?P<pk>[^/]+)/recipedetails', views.recipedetails.as_view(), name='recipe_details),

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.

class addrecipe(FormView):
    form_class = AddRecipeForm
    model = Recipe
    template_name = 'recipebook/addrecipe.html'
    fields = '__all__'
    extra_context = {
        'recipe_list': Recipe.objects.all()
    }
    
    #New method
    def get_success_url(self):
        test_recipe_id = self.object.id #gets id from created object
        return reverse('recipeBook:recipe_details', pk=test_recipe_id)

Your detail url is not receiving the parameter as expected hence it needs to be reconfigured with a new regex
Old:

path('<int:pk>/recipedetails', views.recipedetails.as_view(), name='recipe_details'),

New:

from django.urls import path, re_path

re_path(r'(?P<pk>[^/]+)/recipedetails', views.recipedetails.as_view(), name='recipe_details),
走过海棠暮 2025-02-06 08:11:25

我需要使用httpresponsedirect正确重定向。我的视图最终看起来像这样:

class addrecipe(FormView):
    form_class = AddRecipeForm
    model = Recipe
    template_name = 'recipebook/addrecipe.html'
    fields = '__all__'
    extra_context = {
        'recipe_list': Recipe.objects.all()
    }
    
    def form_valid(self, form):
        test_recipe = form.save(commit=False)
        test_recipe.save()
        test_recipe_id = test_recipe.id
        return HttpResponseRedirect(reverse('recipeBook:recipe_details', kwargs={'pk': test_recipe_id}))

在抓取ID之前保存对象似乎是一个必要的步骤,因为我发现仅在创建对象时创建ID本身。

相反的回报不起作用,所以老实说,我在前面称呼玛丽的httpresponsedilect,它起作用了。如果我弄清楚原因,我将更新答案。

I needed to use HttpResponseRedirect to redirect correctly. My view ended up looking like this:

class addrecipe(FormView):
    form_class = AddRecipeForm
    model = Recipe
    template_name = 'recipebook/addrecipe.html'
    fields = '__all__'
    extra_context = {
        'recipe_list': Recipe.objects.all()
    }
    
    def form_valid(self, form):
        test_recipe = form.save(commit=False)
        test_recipe.save()
        test_recipe_id = test_recipe.id
        return HttpResponseRedirect(reverse('recipeBook:recipe_details', kwargs={'pk': test_recipe_id}))

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文