首页 \ 问答 \ 函数与模板中的参数。(Function with arguments in a template. Django)

函数与模板中的参数。(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
更新时间:2022-05-26 21:05

最满意答案

你试过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'));

相关问答

更多
  • 这应该就是你现在正在做的事情。 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需要连接值,所以尝试类似的东西。 CurrentDb.Execute "INSERT INTO watchhistory (movieID, customer_mail_adress, watch_date, price) " & _ "VALUES (" & Me.txtMovieID & ", '" & Me.txtEmail & "', " & Format(Date(), "\#mm\/dd\/yyyy\#") & ", 2)" 我冒昧地考虑 ...
  • 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 ...
  • 你可以在一个查询中执行它,如果这是你要问的,也记得逃避字符串以避免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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。