渲染错误:没有找到没有参数的“ph”反向。尝试了 1 种模式:['(?P<answer>[^/]+)/ph\\-data/$']

Posted

技术标签:

【中文标题】渲染错误:没有找到没有参数的“ph”反向。尝试了 1 种模式:[\'(?P<answer>[^/]+)/ph\\\\-data/$\']【英文标题】:Rendering error: Reverse for 'ph' with no arguments not found. 1 pattern(s) tried: ['(?P<answer>[^/]+)/ph\\-data/$']渲染错误:没有找到没有参数的“ph”反向。尝试了 1 种模式:['(?P<answer>[^/]+)/ph\\-data/$'] 【发布时间】:2019-05-23 08:30:21 【问题描述】:

我是编码新手,在这里我正在尝试构建一个网站,该网站只接受用户希望从选定范围内看到的内容,因为我运行服务器时遇到了这个错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 2.1.3
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'zigview']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\user\Desktop\Frouniter\FrounterWeb- postgreDB\zigview\templates\FrounterWeb\index.html, error at line 12
   Reverse for 'ph' with no arguments not found. 1 pattern(s) tried: ['(?P<answer>[^/]+)/ph\\-data/$']
   2 : <html lang="en">
   3 : <head>
   4 :      %load staticfiles%
   5 :      <!-- Mobile resizing  and bootstrap -->
   6 :      %include 'FrounterWeb/Lib-includes/Mobile-Response.html'%
   7 : 
   8 :              <!-- javascript action-->
   9 :      %include 'FrounterWeb/Lib-includes/JavaScript-action.html'%
   10 : 
   11 :                 <!-- Libary of chart and gage  -->
   12 :         %include 'FrounterWeb/Lib-includes/Gr aphs-files.ht ml'%
   13 : 
   14 :         <link rel="shortcut icon" type="image/png" href="% static 'FrounterWeb/img/Logo.png' %">
   15 :         <title>Frounter-Agrotech monitoring</title>
   16 : 
   17 : 
   18 : 
   19 : 
   20 :     </head>
   21 : 
   22 : <body class="body" style="background-color:#f6f6f6">


Traceback:

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  154.                 response = response.render()

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  171.                     return self._render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in _render
  163.         return self.nodelist.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
  309.                 return nodelist.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render
  937.                 bit = node.render_annotated(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\base.py" in render_annotated
  904.             return self.render(context)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\template\defaulttags.py" in render
  442.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\base.py" in reverse
  90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  622.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'ph' with no arguments not found. 1 pattern(s) tried: ['(?P<answer>[^/]+)/ph\\-data/$'] 

对不起,我问了这么简单的问题

我尝试过的:

    尝试切换到 pk: 到

    尝试添加到 slug 或

我看了这个网址链接:Django - getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using % url "music:fav" %

我的代码:

我的观点代码:

    try:
     class index(ListView):
     model = tank_system
     template_name = 'FrounterWeb/extends/includes.html'
     ordering = ['-datetime']

    def get_context_data(self,**kwargs):
        return'tank':self.get_queryset()
    #def get_queryset(self):
     #   return

     except:
   logger.error('index page request failed/errors')


 try:
class ph(ListView):
    model = tank_system
    template_name = 'FrounterWeb/extends/ph.html'
    context_object_name = tank_system
    ordering = ['-datetime'] # sort dates in descending order

    def post(self, request, answer):
        if request.method == 'POST':
            form = YourForm(request.POST)

            if form.is_valid():
                answer = form.cleaned_data['value']


    def get_context_data(self, **kwargs):
        #do a selection of which path to do 
        return'tank': self.get_queryset()

    except:
     logger.error('PH page Functions failed/errors')

应用 URl 代码:

        from django.urls import path
        from . import views #function views
        path(r'<str:answer>/ph-data/', login_required(views.ph.as_view()), name='ph'),

我的 HTML 代码供用户输入数据:

%block Buttonbar%

<div class="container-fluid">
    <div class="mx-auto">
      <nav class="navbar navbar-dark bg-primary">

          <div class = "container-fluid" >
          <form action="% url 'zigview:ph-data' answer %" method = "post">
<select class="custom-select" id="datetimefilter" name = "dropdown">
  <option selected="selected" disabled>Select:</option>
    <option value="Today">Today</option>
  <option value="Yestday">Yestday</option>
  <option value="ThWeek">This Week</option>
  <option value="PreWeek">Previous Week</option>
  <option value="ThMonths">This Months</option>
  <option value="PerMonths">Previous Month</option>
  <option value="Thisyear">This Year</option>
  <option value="Lastyear">Last Year</option>
    </select>
        % csrf_token %
        <input type="submit" value="post" name="submit-select">

          <div class="container-fluid">
  <a>Select Range Dates:<input  class="form-control" type="date"/> to <input  class="form-control" type="date"/></a>
  </div>

    </form>
            </div>
      </nav>
    </div>
</div>
%endblock Buttonbar%

总结什么结果:

我希望能够接收变量,也许现在可以从控制台看到,这个变量将被传递到 python 脚本并使用它来选择数据、月份和年份来查看

【问题讨论】:

【参考方案1】:

您的问题似乎是当您的模板呈现时 answer 未定义(我没有看到它添加到任何上下文中)。在answer 中获得值后,您应该能够执行以下操作:

% url 'zigview:ph-data' answer=answer %

更新:OP 不知道如何将answer 添加到他的上下文字典中,所以我将在这部分进行扩展。要将其他值添加到上下文字典中,请将它们添加到基于类的视图的 get_context_data 方法中:

class ph(TemplateView):
    ...
    def get_context_data(self, **kwargs):
        # do a selection of which path to do 
        return 
            'tank': self.get_queryset(),
            'answer': 42,
        
        # the answer that we use above will be used as the parameter to the url in the template.

【讨论】:

您是否将answer 添加到ph.get_context_data 的上下文字典中? 我不这么认为,我该如何添加,对不起:(

以上是关于渲染错误:没有找到没有参数的“ph”反向。尝试了 1 种模式:['(?P<answer>[^/]+)/ph\\-data/$']的主要内容,如果未能解决你的问题,请参考以下文章

Django 3.0:没有找到没有参数的“产品”的反向。尝试了 1 种模式:['product/(?P<slug>[^/]+)/$']

Django URL 配置错误

Django 没有反向匹配错误 - 模板渲染

没有找到没有参数的“forum_post”反向。尝试了 1 种模式:['forumpost/(?P<pk>[0-9]+)/$'] 我该如何解决这个问题?

渲染带有 uid/token 变量的电子邮件模板时没有反向匹配

我怎样才能避免在 django 中没有反向匹配