Custom Admin Form for listing all url

22 views
Skip to first unread message

Vanni Brutto

unread,
Nov 2, 2013, 2:35:19 PM11/2/13
to django...@googlegroups.com
hi,

i want to make a custom form in admin.py for modify the form of a model that should show a combobox with all urlpattern name defined in urls.py.

I tryed with this code (i'm a python/django newbie, sorry ;)):

def fasullo():
    pass

class NameURLForm(forms.ModelForm):
    class Meta:
        model = NameURL
        fields = ['nameStart', 'nameEnd']
    def clean_date(self):
        pass
    def __init__(self, *args, **kwargs):
        super(NameURLForm, self).__init__(*args, **kwargs)

        listURL = []
        for name, L in get_resolver(None).reverse_dict.items():
            if type(name) != type(fasullo):
                if name != None and name != "":
                    listURL.append(name)
        self.fields['nameStart'] = forms.ChoiceField(choices=listURL)
        self.fields['nameEnd'] = forms.CharField(label="nameEnd", max_length=80,required=True,
                                                 widget=forms.Select(choices=[(i,listURL[i]) for i in range(len(listURL))]))


any hint?

Vanni Brutto

unread,
Nov 3, 2013, 1:14:10 PM11/3/13
to django...@googlegroups.com
solved! Here the code:

class NameURLForm(forms.ModelForm):
    class Meta:
        model = NameURL
    def __init__(self, *args, **kwargs):
        super(NameURLForm, self).__init__(*args, **kwargs)

        listURL = []
        for name, L in get_resolver(None).reverse_dict.items():
            if type(name) is not FunctionType:
                listURL.append((name, name))
        listURL.sort()
        self.fields['nameStart'] = forms.ChoiceField(choices=listURL,label=_("URL di partenza"))
        self.fields['nameEnd'] = forms.ChoiceField(choices=listURL,label=_("URL di arrivo"))
Reply all
Reply to author
Forward
0 new messages