MTV新手与Python Django的问题

MTV新手与Python Django的问题,python,django,Python,Django,我对MVC和MTV的概念有点陌生,我正在尝试学习Python Django。我想与图书目录,必须有添加和删除功能。我试图遵循最佳实践并使用通用视图,但最后有点卡住了,我觉得我错过了一些很小的东西,但我无法理解——老实说,我现在非常疲惫,没有太多时间。这是我的代码: 型号: 观点: # coding: utf-8 from django.core.urlresolvers import reverse_lazy from django.views.generic import ListView,

我对MVC和MTV的概念有点陌生,我正在尝试学习Python Django。我想与图书目录,必须有添加和删除功能。我试图遵循最佳实践并使用通用视图,但最后有点卡住了,我觉得我错过了一些很小的东西,但我无法理解——老实说,我现在非常疲惫,没有太多时间。这是我的代码: 型号:

观点:

# coding: utf-8

from django.core.urlresolvers import reverse_lazy
from django.views.generic import ListView, UpdateView, CreateView, DetailView


from models import Book


class BooksDetailView(DetailView):
    model = Book
    template_name = "books_portal/details.html"


class BooksCreateView(CreateView):
    model = Book
    template_name = "books_portal/add.html"
    success_url = reverse_lazy('books_portal')


class BooksUpdateView(UpdateView):
    model = Book
    template_name = "books_portal/add.html"
    success_url = reverse_lazy('books_portal')


class BooksListView(ListView):
    model = Book
    context_object_name = 'books_list'
    template_name = "books_portal/index.html"

    def get_queryset(self):
        return Book.objects.order_by('author')[:5]
模板: add.html

{%extensed“books\u portal/base.html”%}
{%block title%}添加书籍{%endblock%}
{%block extracss%}
.top缓冲区{margin top:20px;}
.bs docs nav{背景色:#563d7c;}
{%endblock extracss%}
{%block content%}
{%csrf_令牌%}
{{form.title.label_tag}
{{form.author.label_tag}}
{%endblock%}
base.html

<!DOCTYPE html>
    <html>
        <head>
            <title>{% block title %}{{title|default:"Book Library"}}{% endblock %}</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            {% block extracss %}{% endblock extracss %}
        </head>
        <body>
            <div class="container">
                <div class="navbar-header">
                    <a href="{% url 'books_portal' %}" class="navbar-brand">Books Portal</a>
                </div>
                {% block content %}
                {% endblock %}
            </div>

            {% block extrajs %}{% endblock extrajs %}
        </body>
    </html>

{%block title%}{{title}默认值:“图书库”}{%endblock%}
{%block extracss%}{%endblock extracss%}
{%block content%}
{%endblock%}
{%block extrajs%}{%endblock extrajs%}
details.html

{% extends "books_portal/base.html" %}

{% block title %}Details{% endblock %}

{% block extracss %}
<style>
    .top-buffer { margin-top:20px; }
    .bs-docs-nav { background-color: #4CD085; }
</style>
{% endblock extracss %}

{% block content %}
         <div class="row top-buffer">
                <div class="col-md-1">
                    <strong>Title:</strong>
                </div>
                <div class="col-md-2">
                    {{book.title}}
                </div>
            </div>
            <div class="row top-buffer">
                <div class="col-md-1">
                    <strong>Author:</strong>
                </div>
                <div class="col-md-2">
                    {{book.author}}
                </div>
            </div>
            <div class="row top-buffer">
                </div>
        <div class="row">
            <div class="col-md-1 col-md-offset-1 text-center"><a href="{% url 'books_portal' %}" class="btn btn-primary btn-lg">OK</a></div>
        </div>
{% endblock %}
{%extensed“books\u portal/base.html”%}
{%block title%}详细信息{%endblock%}
{%block extracss%}
.top缓冲区{margin top:20px;}
.bs文档导航{背景色:#4CD085;}
{%endblock extracss%}
{%block content%}
标题:
{{book.title}}
作者:
{{book.author}}
{%endblock%}
index.html

{% extends "books_portal/base.html" %}

    {% block title %}Collection of books{% endblock %}

    {% block extracss %}
    <style>
        .top-buffer { margin-top:20px; }
        .bs-docs-nav { background-color: #563d7c; }
    </style>
    {% endblock extracss %}

    {% block content %}
        <table class="table table table-hover">
            <tr>
                <th class="text-center">Title</th>
                <th class="text-center">Author</th>
                <th class="text-center">Edit</th>
            </tr>
            {% for book in books_list %}
            <tr>
                <td class="text-center"><a href="{% url 'books_details' pk=book.id%}">{{ book.title }}</a></td>
                <td class="text-center">{{ book.author }}</td>
                <td class="text-center"><a href="{% url 'books_edit' pk=book.id%}" class="btn btn-default">Delete</a></td>
            </tr>
            {% endfor %}
        </table>
        <div class="row">
            <div class="col-md-2 col-md-offset-5 text-center"><a href="{% url 'books_add' %}" class="btn btn-primary btn-lg">Add</a></div>
        </div>
    {% endblock %}
{%extensed“books\u portal/base.html”%}
{%block title%}书籍集合{%endblock%}
{%block extracss%}
.top缓冲区{margin top:20px;}
.bs docs nav{背景色:#563d7c;}
{%endblock extracss%}
{%block content%}
标题
作者
编辑
{图书中图书的百分比\u列表%}
{{book.author}}
{%endfor%}
{%endblock%}

目前我不能添加或删除任何书籍,任何帮助将不胜感激。谢谢。

我已将您的所有代码以及相关的URL.py剪切粘贴到新项目中。直接的问题是,在add.html中没有显示任何表单错误。您只需在模板顶部添加
{{form.errors}

完成后,您将看到实际的问题:在创建图书时没有提供所有相关字段。事实上,Django看不到任何字段,因为您没有给出任何输入
name
属性。实际上,您不应该手动创建
input
元素:您应该让Django这样做,因为在重新显示有错误的表单或编辑现有书籍时,Django还负责使用现有值预填充字段。应该是这样的:

{{ form.title.label_tag }}
{{ form.title }}
{{ form.title.errors }}
然后突出的问题是,您没有包括您的模型根据需要处理的
isbn
pages
字段。您可以在模型中将它们设置为
blank=False
,或者在模型表单中使用
排除
列表。无论哪种方式,对于
页面
,在模型字段中都需要
null=False


当然,您也可以将它们添加到表单中。老实说,在你学习的过程中,你现在最容易做的事情就是用
{form.as_p}}
替换
{form.as_p}}}标记中的所有HTML,这样Django就可以为你输出所有内容。

如果我遗漏了什么,我会很乐意添加它,只是不知道是什么。您发布了太多的代码,但从未描述实际问题是什么。到底出了什么问题?你犯了什么错误?好吧,我活该——你是对的,就像我说的我太累了。问题是,我无法弄清楚为了能够添加和删除书籍,必须在何处实现什么逻辑。我的意思是我应该把它放在模型里还是其他地方?你为什么需要逻辑?您已经有了CreateView,这就是添加书籍所需的全部内容。你也可以使用一个。好吧,问题是CreateView不起作用。它不会在ListView中添加任何书籍。非常感谢,伙计。我感谢你对我的问题的奉献。在你的帮助下,我能够“修复”我的noobish代码。
{% extends "books_portal/base.html" %}

    {% block title %}Collection of books{% endblock %}

    {% block extracss %}
    <style>
        .top-buffer { margin-top:20px; }
        .bs-docs-nav { background-color: #563d7c; }
    </style>
    {% endblock extracss %}

    {% block content %}
        <table class="table table table-hover">
            <tr>
                <th class="text-center">Title</th>
                <th class="text-center">Author</th>
                <th class="text-center">Edit</th>
            </tr>
            {% for book in books_list %}
            <tr>
                <td class="text-center"><a href="{% url 'books_details' pk=book.id%}">{{ book.title }}</a></td>
                <td class="text-center">{{ book.author }}</td>
                <td class="text-center"><a href="{% url 'books_edit' pk=book.id%}" class="btn btn-default">Delete</a></td>
            </tr>
            {% endfor %}
        </table>
        <div class="row">
            <div class="col-md-2 col-md-offset-5 text-center"><a href="{% url 'books_add' %}" class="btn btn-primary btn-lg">Add</a></div>
        </div>
    {% endblock %}
{{ form.title.label_tag }}
{{ form.title }}
{{ form.title.errors }}