我正在尝试这样做:在我的网站中,我有一些来自数据库的数据,如下所示:
Math 140
Math 141
CS 320
CS 240
我想这样做:当您选中复选框并按删除时,此主题将从数据库中删除。
Math 140 []
Math 141 []
CS 320 []
CS 240 []
模板.html
<div id="subjects-list">
{% if user.username %}
<ul>
{% if subjects %}
{% for subject in subjects %}
<li>{{ subject.name }}
{% endfor %}
{% else %}
No Subjects for this user
{% endif %}
</ul>
{% else %}
You are in else
{% endif %}
</div>
模型.py
class Subjects(models.Model):
""" Subject Database """
name = models.CharField(max_length=64, unique=False) #Subject Names on left-menu
user = models.ForeignKey(User, unique=False)
def __unicode__(self):
return '%s %s' %(self.name, self.user.username)
视图.py
@login_required
def user_page(request, username):
user = get_object_or_404(User, username=username)
''' Form in the left-menu to create a new subject '''
if 'subject-create-b' in request.POST:
subject_form = SubjectCreationForm(request.POST)
if subject_form.is_valid():
subject = Subjects.objects.get_or_create(name = subject_form.data['name'],
user = user)
return HttpResponseRedirect('.') # redirect to the same page
# later have to be substituted with AJAX
else:
subject_form = SubjectCreationForm()
# Filter Subjects table by currently logged in user
subjects = Subjects.objects.filter(user__exact = user)
return render_to_response('user_page.html',
{'subject_creation_form': subject_form,
'subjects': subjects,
'user': request.user,
},
context_instance=RequestContext(request))
所以我的问题基本上是我该怎么做?
我是否需要添加新表格,而不是将其放在“
或者我是否需要向 mymodels 添加一些新列,如果选中复选框 - 将此列切换为 true,然后在视图中删除所有在新列中为 true 的行。
或者您可以提出一些更好的技术。先感谢您!