我一直在寻找答案,但是其中任何一个对我都很有用。 问题是我想用以下HTML代码显示 non_field_error :
{% if form.non_field_errors %}
<div class="alert alert-danger">{{ form.non_field_errors|join:"<br \>" }}</div>
{% endif %}
这将产生以下结果: The duplicated error, one formatted and the other not
这是我的 forms.py 代码:
user = authenticate(username=username, password=password)
if not user or not user.is_active:
self.add_error(None, "Incorrect credentials")
如何仅显示格式化的字段,而不同时显示两者?
更新:
如果我尝试这种方法:
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% endif %}
我大致遇到相同的问题:The same error. It appears twice
答案 0 :(得分:0)
如果non_field_errors是一个列表,则可以使用django的每种语法,而不是使用
标记将它们连接在一起,例如:
-(void)initialseTimerNotificationData{
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Title";
content.body = @"A Message";
content.sound = [UNNotificationSound defaultSound];
content.accessibilityHint = @"NotificationCategory";
NSURL *imgUrl = [[NSBundle mainBundle] URLForResource:@"notification" withExtension:@"png"];
NSError *attachmentError = nil;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:imgUrl options:@{UNNotificationAttachmentOptionsTypeHintKey: @"kUTTypePNG"} error:&attachmentError];
content.attachments = @[attachment];
[self addNotificationAction];
// category identifier should be unique. As diff
content.categoryIdentifier = @"NotificationCategory";
// convert string to date
NSDateComponents *componenets = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond + NSCalendarUnitNanosecond fromDate:[NSDate date]];
for (int i= 0; i< minuteArrayList.count; i++) {
componenets.hour = 0;
componenets.minute = [listArray objectAtInde:i];
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:componenets repeats:NO];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
NSLog(@":%@", error.localizedDescription);
}
}];
}
还没有测试过,但这就是一般想法,我相信语法正确
答案 1 :(得分:0)
我有相同的探针,我发现是因为我有{{ form }}
django标签,该标签也加载了non_field_errors,所以我不得不像这样手动渲染它
{{ form.non_field_errors }}
{{ form.label }}
{{ form.field_name }}