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

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

我是编码新手,我正在尝试建立一个网站,该网站只接收用户希望从 selected 范围内看到的内容,因为我 运行 服务器我'我遇到这个错误:

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/$'] 

对不起,我问得这么简单

我尝试过的:

  1. 尝试切换到 PK:到

  2. 尝试添加到 slug 或

我读了这个 url link:

我的代码:

我的浏览代码:

    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 脚本中,并将其用于 select 数据、月份和年份以查看

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

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

更新:OP 不知道如何将 answer 添加到他的上下文字典中,所以我将在这部分进行扩展。要向上下文字典添加其他值,请将它们添加到基于 class 的视图的 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.