错误:找不到带有内联表单集的页面(404)django createview

时间:2019-05-05 18:08:01

标签: django python-3.x django-forms inline-formset

Django 2.1 Python 3.6

我正在尝试使用Django的CreateView创建和保存三种形式。然后,我想重定向回父模型的detailview。但是,当我提交表单时,我将被重定向到配方/而不是配方/测试。我收到页面未找到(404)错误,因为配方/不存在。

没有回溯可以帮助我弄清楚发生了什么。有人可以告诉我我在代码中做错了什么吗?

models.py

from django.db import models
from django.utils.text import slugify
from django.urls import reverse 


class Recipe(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.SlugField(default='')

    def save(self, *args, **kwargs):
        self.slug = (slugify(self.title))
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('recipes:RecipeDetail',
                        kwargs = {'slug': self.slug})


class Ingredient(models.Model):
    recipe = models.ForeignKey(Recipe,
                                on_delete = models.CASCADE)
    description = models.CharField(max_length=255)


class Instruction(models.Model):
    recipe = models.ForeignKey(Recipe,
                                on_delete = models.CASCADE)
    number = models.PositiveSmallIntegerField()
    description = models.TextField()

views.py

通过将重定向移动到发布进行了更新。仍然不起作用。

 def get(self, request, *args, **kwargs):
        """
        Handles GET requests and instantiates blank versions of the form
        and its inline formsets.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet()
        instruction_form = InstructionFormSet()
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests, instantiating a form instance and its inline
        formsets with the passed POST variables and then checking them for
        validity.
        """
        self.object = None
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        ingredient_form = IngredientFormSet(self.request.POST)
        instruction_form = InstructionFormSet(self.request.POST)
        if (form.is_valid() and ingredient_form.is_valid() and
            instruction_form.is_valid()):
            return self.form_valid(form, ingredient_form, instruction_form)
        else:
            return self.form_invalid(form, ingredient_form, instruction_form)
        return super(RecipeCreateView, self).form_valid(form, ingredient_form, instruction_form)

    def form_valid(self, form, ingredient_form, instruction_form):
        """
        Called if all forms are valid. Creates a Recipe instance along with
        associated Ingredients and Instructions and then redirects to a
        success page.
        """
        self.object = form.save()
        ingredient_form.instance = self.object
        ingredient_form.save()
        instruction_form.instance = self.object
        instruction_form.save()
        return super(RecipeCreateView, self).form_valid(form, ingredient_form, instruction_form)

    def form_invalid(self, form, ingredient_form, instruction_form):
        """
        Called if a form is invalid. Re-renders the context data with the
        data-filled forms and errors.
        """
        return self.render_to_response(
            self.get_context_data(form=form,
                                  ingredient_form=ingredient_form,
                                  instruction_form=instruction_form))

urls.py

from django.urls import path


from recipes.views import (RecipeCreateView,
                            RecipeDetail)


app_name = 'recipes'


urlpatterns = [
    path('add',
            RecipeCreateView.as_view(),
            name = 'AddRecipe'),


    path('<slug:slug>',
            RecipeDetail.as_view(),
            name = 'RecipeDetail'),

]

template.html

<!DOCTYPE html>
<html>
<head>
    <title>Multiformset Demo</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="   crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.formset/1.2.2/jquery.formset.js"></script>
    <script type="text/javascript">
        $(function() {
            $(".inline.{{ ingredient_form.prefix }}").formset({
                prefix: "{{ ingredient_form.prefix }}",
            })
            $(".inline.{{ instruction_form.prefix }}").formset({
                prefix: "{{ instruction_form.prefix }}",
            })
        })
    </script>
</head>

<body>
    <div>
        <h1>Add Recipe</h1>
        <form action="." method="post">
            {% csrf_token %}
            <div>
                {{ form.as_p }}
            </div>
            <fieldset>
                <legend>Recipe Ingredient</legend>
                {{ ingredient_form.management_form }}
                {{ ingredient_form.non_form_errors }}
                {% for form in ingredient_form %}
                    {{ form.id }}
                    <div class="inline {{ ingredient_form.prefix }}">
                        {{ form.description.errors }}
                        {{ form.description.label_tag }}
                        {{ form.description }}
                    </div>
                {% endfor %}
            </fieldset>
            <fieldset>
                <legend>Recipe instruction</legend>
                {{ instruction_form.management_form }}
                {{ instruction_form.non_form_errors }}
                {% for form in instruction_form %}
                    {{ form.id }}
                    <div class="inline {{ instruction_form.prefix }}">
                        {{ form.number.errors }}
                        {{ form.number.label_tag }}
                        {{ form.number }}
                        {{ form.description.errors }}
                        {{ form.description.label_tag }}
                        {{ form.description }}
                    </div>
                {% endfor %}
            </fieldset>
            <input type="submit" value="Add recipe" class="submit" />
        </form>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为您需要在POST方法(在表单提交时将被调用)的末尾指定对食谱/测试的重定向,否则默认情况下Django将被重定向到食谱/测试。