我目前刚刚开始学习Django的cour Software Engineering课程。我们的老师给了我们一个使用Django应用CRUD的活动。但是,每次单击“提交”时,我的html表中都不会显示任何内容。这是我的代码:
models.py
from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
# Create your models here.
class ClassList(models.Model):
first_name = models.CharField(max_length = 50, blank = False)
last_name = models.CharField(max_length = 50, blank = False)
id_number = models.CharField(max_length = 9, blank = False)
course = models. CharField(max_length = 50, blank = False)
published_date = models.DateTimeField(blank=True, null=True)
is_active = models.BooleanField()
def __str__(self):
return self.last_name
def get_absolute_url(self):
return reverse('server_edit', kwargs={'pk': self.pk})
forms.py
from django import forms
from .models import ClassList
class ListForm(forms.ModelForm):
class Meta:
model = ClassList
fields = ('first_name', 'last_name', 'id_number', 'course',)
admin.py
from django.contrib import admin
from .forms import ListForm
from .models import ClassList
admin.site.register(ClassList)
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.input_student, name='input_student'),
url(r'^list/', views.student_list, name='student_list'),
]
views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import redirect
from .models import ClassList
from django.utils import timezone
from .forms import ListForm
def input_student(request):
if request.method == "POST":
form = ListForm(request.POST)
if form.is_valid():
lists = form.save(commit=False)
lists.published_date = timezone.now()
lists.is_active = True
lists.save()
return redirect('student_list')
else:
form = ListForm()
return render(request, 'crud_app/form.html', {'form': form})
def student_list(request):
lists = ClassList.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
return render(request, 'crud_app/list.html', {'lists': lists})
def edit(request, pk):
lists = get_object_or_404(ClassList, pk=pk)
if request.method == "POST":
form = ListForm(request.POST, instance=lists)
if form.is_valid():
lists.form.save(commit=False)
lists.published_date=timezone.now()
lists.save()
return redirect('student_list')
else:
form = ListForm(instance=lists)
return render(request, {'form': form})
def reg_delete(request, pk):
lists = get_object_or_404(Group, pk=pk)
lists.is_active = False
lists.save()
return redirect('list')
form.html
{% extends "crud_app/home.html" %}
{% block content %}
<form method="POST" class="list-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="add-button">Add Student</button>
<button a href = "{% url "student_list" %}">View List</a></button>
</form>
{% endblock %}
list.html
{% extends "crud_app/home.html" %}
{% block content %}
<table class="table table-bordered">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>ID Number</th>
<th>Course</th>
<th></th>
</tr>
</thead>
<tbody>
{% for lists in object_list %}
{% if lists.is_active == True %}
<tr>
<td>{{ lists.last_name }}</td>
<td>{{ lists.first_name }}</td>
<td>{{ lists.id_number }}</td>
<td>{{ lists.course }}</td>
<td><a href="">Edit</a></td>
<td><a href="">Delete</a></td> -->
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% endblock content %}
任何帮助都将深表感谢。谢谢。
答案 0 :(得分:1)
问题在于:
return render(request, 'crud_app/list.html', {'lists': lists})
您将'list'传递给上下文,而不是'object_list'。
将密钥更改为 object_list ,将有效。
return render(request, 'crud_app/list.html', {'object_list': lists})
或将list.html中的for更改为:
{% for list in lists %}
{% if list.is_active == True %}
<tr>
<td>{{ list.last_name }}</td>
<td>{{ list.first_name }}</td>
<td>{{ list.id_number }}</td>
<td>{{ list.course }}</td>
<td><a href="">Edit</a></td>
<td><a href="">Delete</a></td> -->
</tr>
{% endif %}
{% endfor %}