我有點新的MVC和MTV的概念,我想學習Python的Django。我想用書做目錄,必須有添加和刪除功能。我試圖遵循最佳實踐並使用泛型視圖,但有點卡在最後,我覺得我失去了一些非常小的東西,但我無法弄清楚 - 說實話,我很累這一刻,我沒有很多時間。所以這是我的代碼: 型號:與Python的Django的MTV新手問題
class Books(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
isbn = models.CharField(max_length=200)
pages = models.IntegerField(default=0)
def __unicode__(self):
return self.title
class BooksForm(ModelForm):
class Meta:
model = Books
瀏覽:
# 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
{% extends "books_portal/base.html" %}
{% block title %}Add books{% endblock %}
{% block extracss %}
<style>
.top-buffer { margin-top:20px; }
.bs-docs-nav { background-color: #563d7c; }
</style>
{% endblock extracss %}
{% block content %}
<form action="" method="post" class="form-horizontal" role="form">{% csrf_token %}
<div class="row top-buffer">
<div class="col-md-1">
{{form.title.label_tag}}
<input type="text" value="" class=""/>
</div>
</div>
<div class="row top-buffer">
<div class="col-md-1">
{{form.author.label_tag}}
<input type="text" value="" class=""/>
</div>
</div>
<div class="row top-buffer">
<div class="col-md-2 col-md-offset-1">
<input type="submit" value="Save" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
{% 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>
細節。 H T毫升
{% 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 %}
的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 %}
目前我不能添加或刪除任何書籍,任何幫助將不勝感激。謝謝。
如果我錯過了一些我很樂意添加它,只是不知道是什麼。 –
你已經發布了太多的代碼,但從未描述過實際的問題。實際上出了什麼問題?你會得到什麼錯誤? –
好吧,我配得上 - 你是對的,就像我說我太累了。問題是,我無法弄清楚什麼邏輯和確切必須在哪裏實施,以便能夠添加和刪除書籍。我的意思是我應該把它放在模型還是其他地方? –