使用Django创建可编辑的HTML表 [英] Creating editable HTML tables with Django

查看:135
本文介绍了使用Django创建可编辑的HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Django应用,用户可以在其中制作电影列表。每次用户登录时,他们的电影列表都将显示在表格中。该表格将包含三列:一列是电影的名称,一列是流派,另一列将包含删除按钮,允许用户删除与按钮对应的行。用户可以通过以下方式向表中添加行:填写带有电影名称的文本框,然后从下拉菜单中选择流派,然后按添加按钮。 添加和删除按钮是用户编辑表的唯一方法。

I'm trying to create a Django app where the user can make a list of movies. Each time the user logs in, their list of movies will be presented to them in a table. The table will have three columns: one for the movie's name, one for the genre, and another that will contain delete buttons that will allow the user to delete the row corresponding to the button. The user can add rows to the table by filling in a textbox with the name of the movie, and selecting a genre from a drop down menu, and then pressing an "Add" button. The "Add" and "Delete" buttons are the only way by which the user can edit the table.

是否存在任何用于创建此类可编辑表的Django快捷方式?我以为这可能适合表单集,但是我不知道该怎么做。此外,由于结果似乎与数据库表有关,因此很难用Google搜索 Django表。

Is there any Django shortcuts to creating such an editable table? I thought this might be something that is appropriate for formsets, but I can't figure out how to do it. Also, it has been difficult to google "Django tables" as the results seem to be about database tables.

这是我目前正在尝试使用的模型:

This is the model that I am currently trying to use:

class MovieList(models.Model):
    user = models.ForeignKey(User)
    movie = models.ForeignKey(Movie)

class Movie(models.Model):
    genre = models.ForeignKey(Genre)
    name = models.CharField(max_length=300)

class Genre(models.Model):
    name = models.CharField(max_length=200)

任何帮助将不胜感激。

推荐答案

有几种方法可以解决此问题。尽管它实际上并没有使用快捷方式,但我看不到通过标准视图呈现表格和附加用于向其输入新条目的表单的问题。

There are several ways you can solve this. Though it doesn't really use a shortcut i can't see what's wrong about rendering the table by a standard view and a attach a form for entering new entries to it.

如果您正在寻找快捷方式和其他便利,则 django-tables 可能是

If you are looking for a shortcut and some additional convenience django-tables might be for you.

当然,您可以使用jQuery使过程更加动态,以便人们可以在不重新加载整个页面的情况下添加和删除项目:类似于Django的<管理员中的href = http://djangoadvent.s3.amazonaws.com/advent/002/inlines_624x464.ogv rel = nofollow noreferrer>动态内联。如果您决定采用此路线,则应仔细阅读 django-dynamic-formsets 或jQuery的多个网格插件之一。 jqGrid 命名为一个。

Of course you could use jQuery to make the process even more dynamic so that people can add and delete items without reloading the whole page: Something similar to Django's dynamic inlines in the admin. If you decide to take this route you should take a closer look a django-dynamic-formsets or at one of the several grid-plugins for jQuery. jqGrid to name one.

这篇关于使用Django创建可编辑的HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆