如何在Django中将ID的编辑方法转移到按钮/链接? [英] How to transfer edit method of an ID to a button/links in Django?

查看:101
本文介绍了如何在Django中将ID的编辑方法转移到按钮/链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ulrs.py中的代码

This is the code in my ulrs.py

url(r'^viewguides/detail/(?P<id>\d+)/edit/$', views.post_update, name='update'),

现在我要使它看起来像这样:

Now I want to make it look like this:

<a href="/detail/(?P<id>\d+)/edit" class="btn btn-default btn-lg" style="background-color: transparent;"><font color="Orange"> EDIT </font></a><hr>

以便我可以编辑想要的特定ID。但这给我一个错误。你怎么做呢?或还有其他方法吗?我是Django的新手。

So that I can edit the certain ID that I want to. But it gives me an error. How do you do it? or is there any other way? I'm new to Django.

推荐答案

我只是问了一些有Django经验的同事。答案很简单。

I just asked my colleagues who have some experience in Django. The answer is very simple.

您只需要在 models.py中创建此函数:

you just need to create this function in "models.py" :

def get_absolute_url(self):
    return "/viewguides/detail/%s/" %(self.id)

,然后可以在html中通过以下方式调用它:

and then you can call it in the html by:

<a href="{{obj.get_absolute_url}}edit" class="btn">EDIT</a>

每次单击btn EDIT时,它将带您到具有给定id的以下编辑页面(包含用户的表单/模型的ID)。

Every time you click the btn EDIT, it will take you to the following edit page with the given id (id that contains the forms/models of the user).

这篇关于如何在Django中将ID的编辑方法转移到按钮/链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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