English 中文(简体)
线性化产生的领域——在验证失败时消失
原标题:Fields generated by inlineformset_factory disappears when validation fails

我有简单的图书作者关系。

class Author(models.Model):
    first_name = models.CharField(max_length=125)
    last_name = models.CharField(max_length=125)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

class Book(models.Model):
    title = models.CharField(max_length=225)
    author = models.ForeignKey(Author)

class AuthorForm(forms.ModelForm):
    class Meta:
        model = Author

class BookForm(forms.ModelForm):
    class Meta:
        model = Book

这是我的看法。

def addbook(request):

    BookFormSet = inlineformset_factory(models.Author, models.Book, extra=1)

    if request.method ==  GET :
        author = models.AuthorForm()
        books = BookFormSet()
    else:
        author = models.AuthorForm(request.POST)
        if author.is_valid():
            books = BookFormSet(request.POST)
            if books.is_valid():
                print(books)

    return render_to_response( bookadd.html , locals(), context_instance = RequestContext(request))

我的模板希望这样做。

<form action="/books/add_new" method="post">
      {% csrf_token %}
      <table>
        <tr>
          <td>First name: </td>
          <td>{{ author.first_name }}</td>
          <td>{{author.first_name.errors}}</td>
        </tr>
        <tr>
          <td>Last name</td>
          <td>{{ author.last_name }}</td>
          <td>{{author.last_name.errors}}</td>
        </tr>
      </table>
      {{ books.management_form }}
      {{ books.as_table }}
      <br/>
      <input type="submit" value="Submit" />
    </form>

如果我离开标题领域空白,打上栏,书标题领域消失了,我无法说明这方面的情况。 我希望现场有用户输入的任何数据。

最佳回答

您可以尝试

    author = models.AuthorForm(request.POST)
    books = BookFormSet(request.POST)
    if author.is_valid():

而不是

    author = models.AuthorForm(request.POST)
    if author.is_valid():
        books = BookFormSet(request.POST)
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签