An warware: siffanta maɓallin ƙirar django admin na gyara a cikin filin

Babban matsalar ita ce keɓance maɓallin ƙirar ƙirar django admin a cikin filin na iya haifar da halayen da ba a zata ba.

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 '&lt;a href="/myapp/mymodel/%s"&gt;edit&lt;/a&gt;' % 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 seherenosuchmehoddefindeon yo urmod elcl ass.,,.Anywaayhreish owyocouldoit.: alsostehatihavechangedy your functionnametoeditt buttongetedi tin k() .Saboda edjanganusesmetho dswithstartin gwithgeasacces sorforfie lsinamodele. 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 ba zai yi aiki ba saboda a nan babu irin wannan hanyar da aka bayyana akan tsarin ƙirar ku., .Ko ta yaya , ga yadda za ku iya yi: kuma lura cewa na canza sunan aikin ku don gyara maɓallin () Don samun hanyar haɗin yanar gizo (). Domin django yana amfani da hanyoyin farawa tare da samun azaman masu shiga filayen a cikin samfurin Class… don haka idan baku canza shi ba, django zai gwada kuma ya kira waccan hanyar akan Ajin Model ɗinku maimakon a Class Admin Model… wanda ba zaiyi aiki ba saboda Babu Irin Wannan Hanyar Da Aka Bayyana A Ajin Model ɗinku … ta yaya , Ga Yadda Zaku Iya Yi : kuma lura cewa na canza sunan aikinku don gyara maɓallin () Don samun hanyar haɗin gwiwa (). Domin django yana amfani da hanyoyin farawa tare da samun azaman masu shiga filayen a cikin samfurin Class… don haka idan baku canza shi ba, django zai gwada kuma ya kira waccan hanyar akan Ajin Model ɗinku maimakon a Class Admin Model… wanda ba zaiyi aiki ba saboda Babu Irin Wannan Hanyar Da Aka Bayyana A Ajin Model ɗinku … ta yaya , Ga Yadda Zaku Iya Yi : kuma lura cewa na canza sunan aikinku don gyara maɓallin () Don samun hanyar haɗin gwiwa (). Domin django yana amfani da hanyoyin farawa tare da samun azaman masu shiga filayen a cikin samfurin Class… don haka idan baku canza shi ba, django zai gwada kuma ya kira waccan hanyar akan Ajin Model ɗinku maimakon a Class Admin Model… wanda ba zaiyi aiki ba saboda Babu Irin Wannan Hanyar da Aka Bayyana Akan Yanayinku lClass . .. duk da haka , Ga Yadda Zaku Iya Yi : kuma ku lura da cewa kuna da sunan aikinku don edita tbuttontogetedi tin k(). Saboda edjangouses hanyoyin farawa tare da samun damar shiga filin sinadirai..sofyadodontcanzait,,djangowilltry dakira da tsarin tsarinmuclassinsteadofonyourmodeladminclass..wanda zaiyi aiki saboda yasan hanyar da aka ƙayyade tsarin aikinku..amma za a iya gyara sunan ku, amma kuma za a iya gyara sunan ku.

Yadda ake keɓance Django

1.7

Babu amsa daya-daya-daidai-duk ga wannan tambayar, saboda hanya mafi kyau don siffanta Django zai bambanta dangane da takamaiman bukatunku. Koyaya, wasu shawarwari kan yadda ake keɓance Django a cikin 1.7 sun haɗa da:

1. Yi amfani da samfuran al'ada da ra'ayoyi

Hanya ɗaya don keɓance Django ita ce ta amfani da samfura da ra'ayoyi na al'ada. Wannan yana ba ku damar ƙirƙirar samfuran ku da ra'ayoyi waɗanda za a iya amfani da su don ƙetare ko ƙara tsoffin samfura da ra'ayoyin da Django ya bayar. Wannan na iya zama babbar hanya don daidaita kamanni da jin daɗin gidan yanar gizonku musamman don bukatun ku.

2. Yi amfani da fayilolin saitunan al'ada

Wata hanya don keɓance Django ita ce ta amfani da fayilolin saitunan al'ada. Waɗannan fayilolin sun ƙunshi bayanan daidaitawa game da gidan yanar gizon ku, kamar waɗanne bayanan bayanai yakamata a yi amfani da su, waɗanda URLs yakamata a shiga, da sauransu. Ta amfani da fayil ɗin saiti daban don kowane yanayi (ci gaba, gwaji, samarwa), zaku iya daidaita Django cikin sauƙi daidai yadda kuke so ba tare da canza fayil ɗin saitunan tsoho ba.

3. Yi amfani da ɗakunan karatu na ɓangare na uku da plugins

A ƙarshe, wata hanya don keɓance Django ita ce ta amfani da ɗakunan karatu na ɓangare na uku da plugins. Waɗannan guda ne na software waɗanda ke ba ku damar yin abubuwan da ba su yiwuwa tare da tsoffin ayyukan da Django ke bayarwa. Misali, zaku iya amfani da ɗakin karatu wanda ke ba da tallafi don ƙauran bayanai ko hanyoyin tantancewa. Ta amfani da ɗakunan karatu na ɓangare na uku da plugins, za ku iya tsawaita ayyukan Django fiye da abin da ke cikin akwatin.

Buttons

Maɓalli abu ne na gama gari a aikace-aikacen yanar gizo. Suna ba masu amfani damar yin hulɗa tare da aikace-aikacen ta hanyoyi daban-daban. Misali, ana iya amfani da maɓalli don ƙaddamar da fom, shiga, ko fita aikace-aikacen.

Fields

Filaye su ne ainihin ɓangaren Django. Ana amfani da su don adana bayanai a cikin rumbun adana bayanai. An bayyana filaye a cikin ajin ƙira kuma ana iya samun dama ga sunansu daga ra'ayoyi da sauran kayayyaki. Hakanan ana iya sanya filayen zuwa abubuwa ta amfani da aikin filin ().

Shafi posts:

Leave a Comment