如何使用django向网址添加问号?

时间:2018-08-30 17:59:01

标签: python regex django url django-rest-framework

我有:

urlpatterns = [
        url(r'^tools(?:/\?tool_one=(?P<tool_one>\w+))?/?$', views.ToolsViews.as_view(), name='tools'),
    ]


像这样的url打印:

  

tools / tool_one =袋子


我想在网址中添加一个问号:

  

tools /?tool_one =袋子


我试图写:

ManagedPoolThread #13 22:18:42 INFO  [Data Exchange] Submitting batch to xConnect. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, operation count: 100)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] Exception while submitting batch to xConnect. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange]    at Sitecore.DataExchange.Providers.XConnect.Processors.PipelineSteps.BaseSubmitXConnectBatchStepProcessor.SubmitBatch(String batchDescription, IXdbContext client, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)
   at Sitecore.DataExchange.Providers.XConnect.Processors.PipelineSteps.BaseSubmitXConnectBatchStepProcessor.SubmitBatch(IXdbContext client, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)
   at Sitecore.DataExchange.Providers.XConnect.Processors.PipelineSteps.AddEntityToXConnectBatchStepProcessor.OnOperationsAddedToClient(IEnumerable`1 operations, IXdbContext client, Int32 minBatchSize, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)
   at Sitecore.DataExchange.Providers.XConnect.Models.EntityModel.AddFacets(IEntityReference entity, IXdbContext client, IXConnectClientHelper helper, Action`6 onOperationsAddedToClient, Int32 minBatchSize, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)
   at Sitecore.DataExchange.Providers.XConnect.Models.ContactModel.AddToClient(IXdbContext client, IXConnectClientHelper helper, Action`6 onOperationsAddedToClient, Int32 minBatchSize, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)
   at Sitecore.DataExchange.Providers.XConnect.Processors.PipelineSteps.AddEntityToXConnectBatchStepProcessor.AddEntityToBatch(XConnectClientPlugin plugin, EntityType entityType, EntityModel entityModel, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] One or more errors occurred. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange]    at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Sitecore.DataExchange.Providers.XConnect.Processors.PipelineSteps.BaseSubmitXConnectBatchStepProcessor.SubmitBatch(String batchDescription, IXdbContext client, PipelineStep pipelineStep, PipelineContext pipelineContext, ILogger logger)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] START AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 7, HResult: -2146233088)
---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
One or more operations did not succeed (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
   at Sitecore.XConnect.XdbContext.<ExecuteBatchAsyncInternal>d__79.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sitecore.XConnect.XdbContext.<SubmitAsync>d__74.MoveNext()
START AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 8, HResult: -2146233088)
---- ---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
Operation #97, AlreadyExists, Contact {8b731842-d710-0600-0000-055b13424422}, EmailAddressHistory (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
---- ---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
Operation #97, AlreadyExists, Contact {8b731842-d710-0600-0000-055b13424422}, EmailAddressHistory (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
END AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 8, HResult: -2146233088)

---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
One or more operations did not succeed (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
   at Sitecore.XConnect.XdbContext.<ExecuteBatchAsyncInternal>d__79.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sitecore.XConnect.XdbContext.<SubmitAsync>d__74.MoveNext()
START AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 9, HResult: -2146233088)
---- ---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
Operation #97, AlreadyExists, Contact {8b731842-d710-0600-0000-055b13424422}, EmailAddressHistory (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
---- ---- An error occurred when trying to add the entity to the xConnect client. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, entity model internal description: )
Operation #97, AlreadyExists, Contact {8b731842-d710-0600-0000-055b13424422}, EmailAddressHistory (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8)
END AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 9, HResult: -2146233088)

END AGGREGATE EXCEPTION (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, ID: 7, HResult: -2146233088)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] Pipeline step processing will abort because a critical error occurred during processing. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch)

ManagedPoolThread #13 22:18:42 DEBUG [Data Exchange] Pipeline processor finished processing pipeline steps. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, completed pipeline steps: 3)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] Sub pipeline processing will abort because a critical error occurred during processing. (pipeline: Process Single Contact from MongoDB Pipeline, pipeline step: Add Contact Model to xConnect Batch, pipeline step identifier: e883b46a-25a9-4d57-b294-de272c0384f8, sub-pipeline: Process Single Contact from MongoDB Pipeline, sub-pipeline processor: Sitecore.DataExchange.Processors.Pipelines.PipelineProcessor)

ManagedPoolThread #13 22:18:42 INFO  [Data Exchange] 2124 elements were iterated before the processor was interrupted. (pipeline: Read Contacts from MongoDB Pipeline, pipeline step: Iterate MongoDB Contacts and Run Pipelines, pipeline step identifier: 3641afc1-3d59-4cde-8263-0ec9b362fa9b)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] Pipeline step processing will abort because a critical error occurred during processing. (pipeline: Read Contacts from MongoDB Pipeline, pipeline step: Iterate MongoDB Contacts and Run Pipelines)

ManagedPoolThread #13 22:18:42 DEBUG [Data Exchange] Pipeline processor finished processing pipeline steps. (pipeline: Read Contacts from MongoDB Pipeline, pipeline step: Iterate MongoDB Contacts and Run Pipelines, completed pipeline steps: 2)

ManagedPoolThread #13 22:18:42 ERROR [Data Exchange] Pipeline processing will abort because a critical error occurred during processing. (pipeline batch: MongoDB Contacts to xConnect Migration, pipeline: Read Contacts from MongoDB Pipeline, pipeline processor: Sitecore.DataExchange.Processors.Pipelines.PipelineProcessor)

ManagedPoolThread #13 22:18:42 DEBUG [Data Exchange] Pipeline batch processor finished processing pipelines. (pipeline batch: MongoDB Contacts to xConnect Migration, completed pipelines: 0)

但问号不存在,URL的打印:

  

tools /%3Ftool_one =袋子

有人可以给我一些提示吗?有什么问题吗?

1 个答案:

答案 0 :(得分:2)

您在这里描述的是 querystring 。查询字符串不是由Django URL处理的。但是,您可以通过基于函数的视图中的request.GET或基于类的视图中的self.request.GET访问查询字符串的数据。

request.GETQueryDict。它就像字典一样(除了它是不可变的,而且键可以映射到多个值)。

因此,您可以使用tool_one获得self.request.GET['tool_one']的值(与查询字典的方式相同)。

由于这不是Django URL的一部分,因此urls.py文件看起来像:

#  app/urls.py

urlpatterns = [
    url(r'^tools/$', views.ToolsViews.as_view(), name='tools'),
]

然后在视图中,例如,可以使用它进行过滤,例如:

class ToolsViews(ListView):
    model=Tool

    def get_queryset(self):
        if 'tool_one' in self.request.GET:
            return Tool.objects.filter(one=self.request.GET['tool_one'])
        return Tool.objects.all()

最后,您可以使用以下模板在模板中构建此类URL:

<a href="{% url 'tools' %}?tool_one=foo">Link</a>

或通过使用<form>方法编写"get"

<form action="{% url 'tools' %}" method="get">
  Tool one: <input type="text" name="tool_one"><br>
  <input type="submit" value="Submit">
</form>

带有method="get"的这种形式会将文本框中的值转换为查询字符串。