主要问题是在字段中自定义 django 管理编辑模型按钮可能会导致意外行为。
I have a model with a foreign key to another model. In the admin view, I want to customise the edit button for this field.
In my admin.py:
<code>class MyModelAdmin(admin.ModelAdmin):
def edit_button(self, obj):
return '<a href="/myapp/mymodel/%s">edit</a>' % obj.id
edit_button.allow_tags = True
edit_button.short_description = 'Edit'
list_display = ('name', 'edit_button')
</code>
This works fine and gives me an "edit" link in the column for each row of my list view, but it links to /myapp/mymodel/1 instead of /admin/myapp/mymodel1/. How can I make it link to the admin page?
A:
You can use <code>reverse()</code>: https://docs.djangoproject.com/en/dev/_modules/django/core/urlresolvers/#reverse (or <code>urlresolvers</code>, which is what reverse is imported as) It will automatically generate urls based on your urls file and any named url patterns you have defined in there (like <code>admin:index</code>) So you could do something like this: (I'm assuming your app name is "foo") If not, just change it accordingly in the code below... Also note that I've changed your function name from <code>edit_button()</code> to <code>get_edit_link()</code>. This is because Django uses methods starting with get as accessors for fields in a model class... So if you don't change it, Django will try and call that method on your model class instead of on your ModelAdmin class... Which won't work because there's no such method defined on your model class... Anyway, here's how you could do it: (Note that I've also added an import statement at the top) Also note that I've changed your function name from <code>edit_button()</code> to <code>get_edit_link()</code>. This is because Django uses methods starting with get as accessors for fields in a model class... So if you don't change it, Django will try and call that method on your model class instead of on your ModelAdmin class... Which won't work because there's no such method defined on your model class... Anyway, here's how you could do it: (Note that I've also added an import statement at the top) Also note that I've changed your function name from edit button () To get edit link (). Because django uses methods starting with get as accessors for fields in a model Class ... so if You don't change It , django Will Try And Call That Method On Your Model Class Instead Of On Your Model Admin Class ... which Won't Work Because There Is No Such Method Defined On Your Model Class ... anyway , Here Is How You Could Do It : ( Note That i Have Also Added An Import Statement At The Top ) Also Note That i Have Changed Your Function Name From Edit Button () To Get Edit Link (). Because django Uses Methods Starting With Get As Accessors For Fields In A Model Class ... so if You Don't Change It , django Will Try And Call That Method On Your Model Class Instead Of On Your Model Admin Class ... which Won't Work Because There Is No Such Method Defined On Your Model Class ... anyway , Here Is How You Could Do It : ( Note That i Have Also Added An Import Statement At The Top ) Also Note That i Have Changed Your Function Name From Edit Button () To Get Edit Link (). Because django Uses Methods Starting With Get As Accessors For Fields In A Model Class ... so if You Don't Change It , django Will Try And Call That Method On Your Model Class Instead Of On Your Model Admin Class ... which Won't Work Because There Is No Such Method Defined On Your Mode lClass . .. anyway , Here Is How You Could Do It : ( Note That i Have Also Added An Import Statement At The Top ) also note that i have changed y our function name from edit button () To get e dit l ink (). B ecause d jango u ses m ethods s taring w ith g et a s a ccessors f or f ields i n a m odel c lass . .. s o if y ou d ont c hange i t , d jangow ill t ry and c all t hat m ethod o n y our m odel c lass i nstead o f o n y our m odel ad min c lass . .. w hich w ont w ork b ecause t hereisn os uchm ethodd efinedo ny ourm odelc lass . .. anyway , h ereis howyoucoulddoit : also note th ati hav ech angedyourfunctionnameto edi tbuttontogetedi tin k(). Becaus edjangousesmethodsstartingwithgetasaccessorsoffieldsinamodelclass..soifyoudontchangeit,,djangowilltryandcallthatmethodonyourmodelclassinsteadofonyourmodeladminclass..whichwontworkbecausehereisnosuchmethoddefinedonyourmodeldefault..anyway,,hereishowyoucoulddoit.: also note th ati hav ech angedyourfunctionnameto edi tbuttontogetedi tin k(). Becaus edjangousesmethodsstartingwithgetasaccessorsoffieldsinamodelclass..soifyoudontchangeit,,djangowilltryandcallthatmethodonyourmodelclassinsteadofonyourmodeladminclass..whichwontworkbecausehereisnosuchmethoddefinedonyourmodeldefault..anyway,,hereishowyoucoulddoit.: also note th ati hav ech angedyourfunctionnameto edi tbuttontogetedi tin k(). Becaus edjangousesmethodsstartingwithgetasaccessorsoffieldsinamodelclass..soifyoudontchangeit,,djangowilltryandcallthatmethodonyourmodelclassinsteadofonyourmodeladminclass.,,.anyway,,hereishowyoucoulddoit.: also note th ati hav ech angedyou rfunct ionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl assinsteadofonyourmodela mincl ass.,,.Whichwotnworkbecau seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsonotehatihavechangedyourfunctionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl assinsteadofonyourmodela mincl ass.,,.Whichwotnworkbecau seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsonotehatihavechangedyourfunctionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl assinsteadofonyourmodela mincl ass.,,.Whichwotnworkbecau seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsonotehatihavechangedyourfunctionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl assinsteadofonyourmodela minc lassthenwillnotbeabletofindthismehodinyourapplicationbecauseitsnotdefinedintheurlpatternsyourapplicationhasbeenconfiguredtousebutthiswillstillgiveyouideahoweasyitisusethebuiltinf
unctionsdjangoprovidesforyouinordertogenerateurlsinyourapplication.,,.Whichwotnworkbecau seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsonotehatihavechangedyourfunctionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl assinsteadofonyourmodela minc lassthenwillnotbeabletofindthismehodinyourapplicationbecauseitsnotdefinedintheurlpatternsyourapplicationhasbeenconfiguredtousebutthiswillstillgiveyouideahoweasyitisusethebuiltinfunctionsdjangoprovidesforyouinordertogenerateurlsinyourapplication.,,.Whichwotnworkbecau sehereenosuchmehoddefindedeon yo urmod elcl ass.,,.Anywaayhreish owyoculdoit.: alsonotehatihavechangedyourfunctionnametoedittbuttongeteditink(). 因为 edjanganusesmethodwithstartingwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delathecl assin bletofindthismehodinyourapplicationbecauseitsnotdefinedintheurlpatternsyourapplicationhasbeenconfiguredtousebutthiswillstillgiveyouideahoweasyitisusethebuiltinfunctionsdjangoprovidesforyouinordertogenerateurlsinyourapplication.,,.Whichwotnworkbecau seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsonotehatihavechangedyourfunctionnametoeditt buttongetedi tin k().Becaus edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodelelass.,,.Soifdondtchan geittryan calllhatmet ho donyourmo delcl classinstead ofony ourmodel adminclass ., .which不会工作,因为这里没有在你的模型类上定义这样的方法。,。无论如何,这是你如何做到的:另请注意,我已将你的函数名称更改为编辑按钮()以获取编辑链接()。 因为 django 使用以 get 开头的方法作为模型类中字段的访问器……所以如果您不更改它,django 将尝试在您的模型类而不是模型管理类上调用该方法……这是行不通的,因为你的模型类上没有定义这样的方法......无论如何,这是你如何做到的:另请注意,我已将你的函数名称更改为编辑按钮()以获取编辑链接()。 因为 django 使用以 get 开头的方法作为模型类中字段的访问器……所以如果您不更改它,django 将尝试在您的模型类而不是模型管理类上调用该方法……这是行不通的,因为你的模型类上没有定义这样的方法......无论如何,这是你如何做到的:另请注意,我已将你的函数名称更改为编辑按钮()以获取编辑链接()。 因为 django 使用以 get 开头的方法作为模型类中字段的访问器……所以如果您不更改它,django 将尝试在您的模型类而不是模型管理类上调用该方法……这是行不通的,因为您的模型 lClass 上没有定义此类方法。 .. 无论如何,这里是你如何做到的:另请注意,我已经将你的函数名称更改为编辑按钮以获取 tin k()。 因为 edjango 使用方法从 getasaccessorsoffieldsina modelclass..so 如果你不改变它,django 将尝试在你的模型类上调用该方法而不是在你的模型管理类上..这不会工作,因为这里没有在你的模型默认上定义这样的方法..无论如何,这里显示你可以这样做。:还要注意,我已经将你的函数名称更改为编辑按钮以获取 tin k()。 因为 Django 使用以 `get` 开头的方法作为模型类中字段的访问器……所以如果你不修改它,Django 会尝试在你的模型类中调用该方法,而不是在你的模型管理类中调用……这将不起作用,因为模型管理类中没有定义该方法。
如何自定义 Django
1.7
这个问题没有千篇一律的答案,因为自定义 Django 的最佳方式会因您的具体需求而异。 但是,关于如何在 1.7 中自定义 Django 的一些技巧包括:
1.使用自定义模板和视图
自定义 Django 的一种方法是使用自定义模板和视图。 这允许您创建自己的模板和视图,这些模板和视图可用于覆盖或补充 Django 提供的默认模板和视图。 这是专门根据您自己的需要定制网站外观和感觉的好方法。
2.使用自定义设置文件
自定义 Django 的另一种方法是使用自定义设置文件。 这些文件包含有关您网站的配置信息,例如应使用哪些数据库、应访问哪些 URL 等。 通过为每个环境(开发、测试、生产)使用单独的设置文件,您可以轻松地完全按照您想要的方式配置 Django,而无需修改默认设置文件。
3.使用第三方库和插件
最后,另一种自定义 Django 的方法是使用第三方库和插件。 这些软件可以让你做一些 Django 本身提供的默认功能做不到的事情。 例如,您可以使用为数据迁移或身份验证机制提供支持的库。 通过使用第三方库和插件,您可以扩展 Django 的功能,使其超出开箱即用的范围
钮扣
按钮是 Web 应用程序中的常见元素。 它们允许用户以各种方式与应用程序交互。 例如,按钮可用于提交表单、登录或退出应用程序。
字段
字段是 Django 的核心组件。 它们用于将数据存储在数据库中。 字段在模型类中定义,可以通过名称从视图和其他模块访问。 也可以使用 field() 函数将字段分配给对象。