我有一个Django模型表单,如果在另一个表单域中选择了一些值,我想在其中创建一个表单域。
例如 如果在activity_name表单字段中选择了activity_name =“项目计划的活动”,则必须要求project_id字段= true
我为此添加了一个表单验证器,但是它不起作用
class ActivityTrackerModelForm(forms.ModelForm):
activity_name = forms.ModelChoiceField(
queryset=activity.objects.all().order_by('activity_name'),
label='',
empty_label="Select Activity",
widget=forms.Select(
attrs={
'class': 'form-control',
}
)
)
project_id = forms.CharField(
label='',
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "Project ID",
'class': 'form-control'
}
)
)
class Meta:
model = activity_tracker
fields = ['activity_name', 'project_id']
def clean(self):
activity = self.cleaned_data.get('activity_name')
if re.search("^Project.*Activities$", str(activity)):
self.fields['project_id'].required = True
尝试使用表单验证器
如果在activity_name表单字段中选择了activity_name =“项目计划的活动”,则必须输入project_id字段= true
答案 0 :(得分:0)
问题首先出现在我的Validator本身和我看来。我没有将当前表单传递给模板,而是将新表单eveytime传递给了模板。
正确的表单验证器
class ActivityTrackerModelForm(forms.ModelForm):
date = forms.DateField(label='', widget=forms.DateInput(attrs={
"placeholder": "Select Date", 'id': 'datepicker', 'class': 'form-control w-100', 'autocomplete': 'off'}))
activity_name = forms.ModelChoiceField(queryset=activity.objects.all().order_by(
'activity_name'), label='', empty_label="Select Activity", widget=forms.Select(attrs={'class': 'form-control w-100'}))
system_name = forms.ModelChoiceField(queryset=system.objects.all().order_by('system_name'), label='', empty_label="Select System", widget=forms.Select(attrs={'class': 'form-control w-100'}))
client_name = forms.ModelChoiceField(queryset=client.objects.all().order_by(
'client_name'), label='', empty_label="Select Client", widget=forms.Select(attrs={'class': 'form-control w-100'}))
hour_choice = [('', 'Choose Hours'), (0, 0), (1, 1), (2, 2),(3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8)]
hours = forms.ChoiceField(label='', choices=hour_choice, widget=forms.Select(
attrs={'class': 'form-control w-100'}))
min_choice = [('', 'Choose Mins'), (0, 0), (15, 15), (30, 30), (45, 45)]
mins = forms.ChoiceField(label='', choices=min_choice, widget=forms.Select(attrs={'class': 'form-control w-100'}))
no_of_records = forms.IntegerField(label='', required=False, widget=forms.NumberInput(
attrs={"placeholder": "Enter no. of Records", 'class': 'form-control w-100', 'autocomplete': 'off'}))
project_id = forms.CharField(label='', required=False, widget=forms.TextInput(
attrs={"placeholder": "Project ID", 'class': 'form-control w-100'}))
user_comments = forms.CharField(
label='',
required=False,
widget=forms.Textarea(
attrs={
"placeholder": "Enter Your Comments Here...",
'rows': 6,
'class': 'form-control w-100',
'autocomplete': 'off'
}
)
)
class Meta:
model = activity_tracker
fields = ['date', 'activity_name', 'system_name', 'client_name',
'hours', 'mins', 'no_of_records', 'project_id', 'user_comments']
def clean(self):
cleaned_data = super(ActivityTrackerModelForm, self).clean()
activity = cleaned_data.get('activity_name')
project_1 = cleaned_data.get('project_id')
if re.search("^Project.*Activities$", str(activity)) and project_1 == "":
self.add_error('project_id', "Please Add Project ID")
return cleaned_data
正确的视图
def MyTask(request):
form = ActivityTrackerModelForm(request.POST or None)
if request.method == 'POST':
form = ActivityTrackerModelForm(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.user_name = request.user
obj.approver = tascaty_user.objects.get(
username=request.user).approver
if request.user.is_team_lead:
obj.status = activity_status.objects.get(pk=3)
obj.save()
messages.success(request, 'New Entry Created')
return redirect('mytask')
queryset1_PA = activity_tracker.objects.filter(
user_name=request.user).filter(status__in=[1, 2, 4]).order_by('-id')
queryset1_AP = activity_tracker.objects.filter(
user_name=request.user).filter(status=3).order_by('-date')
paginator_RA = Paginator(queryset1_AP, 10)
paginator_PA = Paginator(queryset1_PA, 10)
page = request.GET.get('page')
context = {
'title': 'TasCaty|My Task',
'activity_form': form,
'post_page_RA': paginator_RA.get_page(page),
'post_page_PA': paginator_PA.get_page(page),
}
return render(request, "tascaty/mytask.html", context)