函数与模板中的参数。(Function with arguments in a template. Django)
在我的模板中,我显示用户关注的用户列表。 我希望用户能够通过按钮删除他关注的一个用户。 我有一个删除关系的函数remove_relationship。
这是我的models.py中的函数:
class UserProfile(models.Model): (...) def remove_relationship(self, person): Relationship.objects.filter( from_person=self, to_person=person).delete() return
我想将此函数传递给我的模板:
{% for user in following % } <form method="post"> {% csrf_token %} <input type="submit" value="delete" onclick="remove_relationship"/> </form> {%endfor%}
问题是我无法在模板中传递参数。 那么我怎么能这样做,每个按钮删除与正确用户的关系?
我在这个主题上看到了另一个问题,看起来它似乎无法解决我的问题(http://stackoverflow.com/questions/1333189/django-template-system-calling-a-function-inside-a-model )
感谢您的帮助。
In my template, I display a list of users a user follows. I would like the user to be able to delete one of the users he follows thanks to a button. I have a function remove_relationship that deletes a relationship.
Here is the function in my models.py:
class UserProfile(models.Model): (...) def remove_relationship(self, person): Relationship.objects.filter( from_person=self, to_person=person).delete() return
I would like to pass this function into my template:
{% for user in following % } <form method="post"> {% csrf_token %} <input type="submit" value="delete" onclick="remove_relationship"/> </form> {%endfor%}
The thing is that I can't pass argument in my template. So how can I do so that each button deletes the relationship with the right user?
I saw an other question on this topic, abut it looks like it doesn't solve my problem (http://stackoverflow.com/questions/1333189/django-template-system-calling-a-function-inside-a-model)
Thank you for your help.
原文:https://stackoverflow.com/questions/13233794
最满意答案
你试过
to_date('" & Attendance!C2 & "','HH24:MI:SS')
?这是完整的查询
="INSERT INTO ATTENDANCE (EMPLOYEE_ID, ATT_DATE, ATT_TIME) VALUES('" & Attendance!A2 & "', to_date('" & TEXT(Attendance!B2,"dd-mmm-yyyy") & "','DD-MON-YYYY'),to_date('" & Attendance!C2 & "','HH24:MI:SS'));
Did you try
to_date('" & Attendance!C2 & "','HH24:MI:SS')
?Here is the complete query
="INSERT INTO ATTENDANCE (EMPLOYEE_ID, ATT_DATE, ATT_TIME) VALUES('" & Attendance!A2 & "', to_date('" & TEXT(Attendance!B2,"dd-mmm-yyyy") & "','DD-MON-YYYY'),to_date('" & Attendance!C2 & "','HH24:MI:SS'));
相关问答
更多-
高效的插入语句(Efficient insert statement)[2022-11-25]
这应该就是你现在正在做的事情。 Insert Into Journeys(DepartureID, ArrivalID, ProviderID, JourneyNumber, Active) Select dbo.GetHubIDFromName(StartHubName), dbo.GetHubIDFromName(EndHubName), dbo.GetBusIDFromName(CompanyName), JourneyNo, 1 From Holding OR ... -
不知道至少两个要素是很难给出答案的: 1)您的数据库是否运行在PHP代码所在的同一台服务器上? 2)这个文件有多“大”? 即平均20 csv记录? 200? 20000? 一般情况下,通过循环访问csv文件并为每行启动插入语句(请使用预准备语句 ,或者DB每次花费时间解析相同的字符串)将是更传统的方法,并且效率足够高除非在PHP和数据库之间连接速度非常慢。 即使在这种情况下,如果csv文件超过20条记录长度,您可能会从SQL分析器开始遇到最大语句长度问题。 It is difficult to give a ...
-
访问插入语句(Access insert into statement)[2022-05-12]
使用Access需要连接值,所以尝试类似的东西。 CurrentDb.Execute "INSERT INTO watchhistory (movieID, customer_mail_adress, watch_date, price) " & _ "VALUES (" & Me.txtMovieID & ", '" & Me.txtEmail & "', " & Format(Date(), "\#mm\/dd\/yyyy\#") & ", 2)" 我冒昧地考虑 ... -
插入语句难度(insert statement difficulty)[2023-08-21]
INSERT加WHERE? 我想你需要UPDATE,而不是INSERT http://dev.mysql.com/doc/refman/5.0/en/update.html 编辑:不清楚,你是在一个表中插入与另一个表中的位置混合?,只是在没有WHERE的情况下执行“INSERT ...(fields)VALUES(values)”并在字段上指定所有addressID。 INSERT plus WHERE? i guess you need UPDATE, not INSERT http://dev.mysq ... -
以sql格式导出表 .sql文件的输出格式为表格 创建表语句 插入每个条目语句 所以打开.sql文件并复制所有INSERT语句 希望这是你的要求 Export the table in sql format The Format of Output of .sql file is In the form Create Table Statements Insert each entry statement So Open that .sql file and copy all INSERT statemen ...
-
insert语句应该是 INSERT INTO User_Friend(ProfileId1, ProfileId) VALUES ( @FriendProfileId, (SELECT ProfileId FROM User_Profile WHERE UserId = @UserId)) 或者SELECT TOP(1) ProfileId以确保您永远不会获得超过1的值。 The insert statement should be INSERT INTO User_Friend(P ...
-
你试过to_date('" & Attendance!C2 & "','HH24:MI:SS') ? 这是完整的查询 ="INSERT INTO ATTENDANCE (EMPLOYEE_ID, ATT_DATE, ATT_TIME) VALUES('" & Attendance!A2 & "', to_date('" & TEXT(Attendance!B2,"dd-mmm-yyyy") & "','DD-MON-YYYY'),to_date('" & Attendance!C2 & "','HH24:M ...
-
在外键概念中,无论您尝试在子表中插入/更新什么值,都必须存在于其父/引用表中。 在这里,您尝试在已创建引用的列中插入0,NULL等值,并且主表不保留这些值。 解决方法1: 首先在主表中添加这些值,然后可以在子表中插入。 溶液2: 如果你想根据你的要求强行在子表中添加这样的数据,那么你可以这样做 - set foreign_key_checks=0; Insert statement here... set foreign_key_checks=1; In foreign key concept whate ...
-
多个插入语句(Multiple Insert Statement)[2023-03-30]
你可以在一个查询中执行它,如果这是你要问的,也记得逃避字符串以避免SQL注入: $P_studentnumber=mysql_real_escape_string($_POST['studentnumber']); $P_subjectcode=mysql_real_escape_string($_POST['subjectcode']); $sql="INSERT INTO grades (studentnumber,subjectcode,course) SELECT '$P_studentnumbe ... -
INSERT语句不返回任何记录,那么为什么要尝试将其结果分配给记录集? 改变这一行: Set rs = Conn.Execute("insert into TestTable(TestColumn) Values('50');") 执行: Conn.Execute("insert into TestTable(TestColumn) Values('50');") 然后清理代码以消除不需要的记录集引用。 An INSERT statement doesn't return any records, so ...