当在模板中填充select时,Django选择有效的选择错误

时间:2018-02-15 10:19:56

标签: javascript django django-forms

当我创建一个带有空选择字段的表单时,我收到验证错误:

2018-02-15 12:04:21.797 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.s.s.DefaultHandshakeHandler      : Processing request http://localhost:43755/test-server with headers={authorization=[kate], sec-websocket-key=[xZ67EfJb278L2ERbzCmUDw==], connection=[upgrade], sec-websocket-version=[13], host=[localhost:43755], upgrade=[websocket]}
2018-02-15 12:04:21.797 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.s.s.DefaultHandshakeHandler      : Upgrading to WebSocket, subProtocol=null, extensions=[]
2018-02-15 12:04:21.799 TRACE 15876 --- [cTaskExecutor-1] o.s.w.s.c.s.StandardWebSocketClient      : Handshake response headers: {date=[Thu, 15 Feb 2018 09:04:21 GMT], upgrade=[websocket], connection=[upgrade], sec-websocket-accept=[d7OI16/H5uovwY7a+QxLbuZKuaY=]}
2018-02-15 12:04:21.799 DEBUG 15876 --- [cTaskExecutor-1] o.s.m.simp.stomp.DefaultStompSession     : Connection established in session id=bd60b291-eadd-c8f6-5b38-060d2318debd
2018-02-15 12:04:21.800 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler        : From client: CONNECT session=9
2018-02-15 12:04:21.801 DEBUG 15876 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing CONNECT user=kate session=9
2018-02-15 12:04:21.803 TRACE 15876 --- [-auto-1-exec-10] o.s.w.s.m.StompSubProtocolHandler        : From client: SUBSCRIBE /user/queue/messages id=0 session=9
2018-02-15 12:04:21.803 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing SUBSCRIBE /user/queue/messages id=0 session=9
2018-02-15 12:04:21.803 TRACE 15876 --- [nboundChannel-5] o.s.m.s.u.UserDestinationMessageHandler  : Translated /user/queue/messages -> [/queue/messages-user9]
2018-02-15 12:04:21.803 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing SUBSCRIBE destination=/queue/messages-user9 subscriptionId=0 session=9 user=kate payload=byte[0]
2018-02-15 12:04:21.826 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'd890e685-9049-4773-94a1-f1334da176eb'!
2018-02-15 12:04:21.826 TRACE 15876 --- [MessageBroker-4] o.s.m.s.u.UserDestinationMessageHandler  : Translated /user/kate/queue/messages -> [/queue/messages-user7]
2018-02-15 12:04:21.826 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/queue/messages-user7 session=null payload=Private UUID - 'd890e685-9049-4773-94a1-f1334da176eb'!
2018-02-15 12:04:21.833 TRACE 15876 --- [o-auto-1-exec-1] o.s.w.s.m.StompSubProtocolHandler        : From client: DISCONNECT session=7
2018-02-15 12:04:21.833 DEBUG 15876 --- [nboundChannel-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing DISCONNECT session=7
2018-02-15 12:04:21.834 DEBUG 15876 --- [o-auto-1-exec-1] o.s.w.s.m.SubProtocolWebSocketHandler    : Clearing session 7
2018-02-15 12:04:21.834 DEBUG 15876 --- [nboundChannel-2] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing DISCONNECT session=7
2018-02-15 12:04:21.834 DEBUG 15876 --- [tboundChannel-6] o.s.w.s.m.SubProtocolWebSocketHandler    : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=7}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=7}]
2018-02-15 12:04:21.834 DEBUG 15876 --- [tboundChannel-5] o.s.w.s.m.StompSubProtocolHandler        : Failed to send WebSocket message to client in session 7

java.lang.IllegalStateException: Message will not be sent because the WebSocket session has been closed
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:425) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:309) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendMessageBlock(WsRemoteEndpointImplBase.java:250) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
    at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendPartialString(WsRemoteEndpointImplBase.java:223) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
    at org.apache.tomcat.websocket.WsRemoteEndpointBasic.sendText(WsRemoteEndpointBasic.java:49) ~[tomcat-embed-websocket-8.5.27.jar:8.5.27]
    at org.springframework.web.socket.adapter.standard.StandardWebSocketSession.sendTextMessage(StandardWebSocketSession.java:203) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.adapter.AbstractWebSocketSession.sendMessage(AbstractWebSocketSession.java:101) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.tryFlushMessageBuffer(ConcurrentWebSocketSessionDecorator.java:153) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sendMessage(ConcurrentWebSocketSessionDecorator.java:125) ~[spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.sendToClient(StompSubProtocolHandler.java:471) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageToClient(StompSubProtocolHandler.java:458) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:337) [spring-websocket-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:135) [spring-messaging-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

2018-02-15 12:04:21.928 DEBUG 15876 --- [MessageBroker-4] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '3a223606-3fec-4def-a194-0ac35e17b757'!
2018-02-15 12:04:21.928 TRACE 15876 --- [MessageBroker-4] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.028 DEBUG 15876 --- [MessageBroker-2] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '9213c267-d2c8-44a7-bdc8-5f300220eb31'!
2018-02-15 12:04:22.028 TRACE 15876 --- [MessageBroker-2] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.129 DEBUG 15876 --- [MessageBroker-1] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '196a612b-7689-40e7-b4b3-5819f88844ad'!
2018-02-15 12:04:22.130 TRACE 15876 --- [MessageBroker-1] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.230 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'a4bfb498-aabc-4641-80bb-33a763fb165e'!
2018-02-15 12:04:22.231 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.332 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '37c6a440-2797-4453-9016-2ad1247eed14'!
2018-02-15 12:04:22.332 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.433 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'f333d450-f04f-4e0f-84df-0e310a993938'!
2018-02-15 12:04:22.433 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.533 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '95291a0a-d608-48ba-8f8e-012d64e825ef'!
2018-02-15 12:04:22.534 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.634 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - 'da26c02e-d841-4215-aa00-b42999c30a1b'!
2018-02-15 12:04:22.635 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.736 DEBUG 15876 --- [MessageBroker-3] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing MESSAGE destination=/user/kate/queue/messages session=null payload=Private UUID - '6c6732a2-9598-46cf-9797-113347965165'!
2018-02-15 12:04:22.736 TRACE 15876 --- [MessageBroker-3] o.s.m.s.u.UserDestinationMessageHandler  : No active sessions for user destination: /user/kate/queue/messages
2018-02-15 12:04:22.803 DEBUG 15876 --- [           main] o.s.m.simp.stomp.DefaultStompSession     : Connection closed in session id=bd60b291-eadd-c8f6-5b38-060d2318debd
2018-02-15 12:04:22.804 TRACE 15876 --- [o-auto-1-exec-2] o.s.w.s.m.StompSubProtocolHandler        : From client: UNSUBSCRIBE id=0 session=9
2018-02-15 12:04:22.804 DEBUG 15876 --- [nboundChannel-4] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing UNSUBSCRIBE id=0 session=9
2018-02-15 12:04:22.804 TRACE 15876 --- [o-auto-1-exec-2] o.s.w.s.m.StompSubProtocolHandler        : From client: DISCONNECT session=9
2018-02-15 12:04:22.804 DEBUG 15876 --- [nboundChannel-1] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing DISCONNECT session=9
2018-02-15 12:04:22.805 DEBUG 15876 --- [o-auto-1-exec-2] o.s.w.s.m.SubProtocolWebSocketHandler    : Clearing session 9
2018-02-15 12:04:22.805 DEBUG 15876 --- [nboundChannel-5] o.s.m.s.b.SimpleBrokerMessageHandler     : Processing DISCONNECT session=9
2018-02-15 12:04:22.806 DEBUG 15876 --- [tboundChannel-8] o.s.w.s.m.SubProtocolWebSocketHandler    : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpHeartbeat=[J@47a1a4bc, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}]
2018-02-15 12:04:22.807 DEBUG 15876 --- [tboundChannel-8] o.s.w.s.m.SubProtocolWebSocketHandler    : No session for GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT_ACK, simpDisconnectMessage=GenericMessage [payload=byte[0], headers={simpMessageType=DISCONNECT, stompCommand=DISCONNECT, simpSessionAttributes={org.springframework.messaging.simp.SimpAttributes.COMPLETED=true}, simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}], simpUser=com.example.sockettesting.configuration.WebSocketConfiguration$1$$Lambda$12/293071828@319d0266, simpSessionId=9}]

然后我使用ajax填充模板中的select:

area_sp = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control', 'id':'area_select'}))

最后,当我提交表单时,我收到以下错误:

$.ajax({
  url: '/endpoint/load_areas/',
  type: 'post',
  dataType: 'json',
  data: {
      'postcode': postcode
  },
  headers: {
      'X-CSRFToken': "{{ csrf_token }}"
  },
  success: function (data) {
  var ret = JSON.parse(data.result);

  for (x in ret) {
    $("#area_select").append(new Option(x, ret[x]));
  }
  },
  error: function(data) {
     alert("error");
    }
});

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

看起来你只是忘了定义有效的选择。您必须告诉ChoiceField,否则您将获得ValidationError。关于ChoiceField的文档:

  

验证选择列表中是否存在给定值。

所以只需将choices属性设置为表单接受的内容:

area_sp = forms.ChoiceField(
    widget=forms.Select(
        attrs={'class': 'form-control', 'id':'area_select'}
    ),
    choices=[(value1, repr1), (value2, repr2), ...]
)