【发布时间】:2019-03-20 15:16:30
【问题描述】:
我想检查 URL 路径,以便可以基于它创建表单。在视图中,我们可以使用request参数来获取URL路径,有没有类似的方法可以在forms.py里面获取当前的URL?
URL=??
if URL == 'phones':
brand_choises= (('Sumsung', 'Sumsung'),
('Iphon', 'Iphone'),)
if URL == 'cars':
brand_choises= (('Honda', 'Honda'),
('Toyota', 'Toyota'),)
class Productform(forms.ModelForm):
brand=forms.ChoiceField(choices=brand_choises,widget=forms.Select(attrs={'class':'products'}))
class Meta:
model = Product
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.fields['brand'].choices = brand_choises
【问题讨论】:
-
类似于this
标签: django django-forms django-urls