简体   繁体   中英

Page not found (404) error happens I did not write the url

I got an error,Page not found (404) Request Method: GET Request URL: http://localhost:8000/app/test.html?prop=value . I wrote index.html like

<body>
   <form method="post" action="">
    <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
    {% for i in json_data.items.values %}
            <option value="{{forloop.counter}}">{{ i }}</option>
    {% endfor %}
    </select>

    {% for key, values in preprocessed %}
    <select name="type" id=type{{forloop.counter}}>
    {% for counter, value in values %}
        <option value="{{forloop.counter}}">{{ value }}</option>
    {% endfor %}
    </select>
    {% endfor %}
    </form>

  <script type="text/javascript">

        $(document).ready(function () {
            $('#mainDD').on('change', function() {
              var thisType = "type" + $(this).val();
              for(i=1; i<6; i++) {
                  var thisId = "type" + i;
                  if(thisType !== thisId) {
                    $("#"+thisId).hide();
                  }
                  else {
                    $("#"+thisId).show();
                  }
              }

            }).trigger('change');

        });


  </script>

     <form id="postform" action="http://localhost:8000/app/test_view" method="POST">
      {% csrf_token %}
      <input type="submit" value="SEND">
     </form>
     <script type="text/javascript">
        let key = "prop";
        let value = "value";
        var array1 = [];
        var array2 =[];
          document.querySelector("input[type=submit]").onclick = e => {
           const test = window.open(`test.html?${key}=${value}`, "_blank");
          }

     </script>
  </body>

This part const test = window.open('test.html?${key}=${value}', "_blank"); causes error because I did not regist url http://localhost:8000/app/test.html?prop=value . I only register http://localhost:8000/app/test_view . But I wanna send selected i & value's values to test.html,so I wrote this like. I wrote in test.html like

<body>

<h2>RESULT</h2>
    <script type="text/javascript">

      onload = () => {
        document.body.appendChild(
          document.createTextNode([...new URLSearchParams(location.search).entries()])
          );
      }

    </script>

</body>

How should I fix this?What should I write this?

urls.py is

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^test_view$', views.test_view, name='test_view'),
]

views.py is

def test_view(request):
    return render(request, 'test.html')

My app structure is testapp(parent app) -app(child app) -inde.html&test.html&views.py

testapp's urls.py is

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^app/', include('app.urls')),
]

Now my code is

<form id="postform" action="http://localhost:8000/app/test_view" method="GET">
      {% csrf_token %}
      <a href={% url 'test_view' %}>Your link to test view</a>
     </form>
     <script type="text/javascript">
        let key = "prop";
        let value = "value";
        var array1 = [];
        var array2 =[];
          document.querySelector("input[type=submit]").onclick = e => {
           const test = window.open(`test.html?${key}=${value}`, "_blank");
          }

     </script>

You registred http://localhost:8000/app/test_html but try to go by http://localhost:8000/app/test.html link. Your GET parameters don't make a sense.

You should use url, that you registred in urls.py .
Read this tutorial about views and urls.

Change action attribute in your form. It might looks like that:

In your template your link may looks like this:

<form id="postform" action="{% url 'test_view' %}" method="GET">

or:

<form id="postform" action="app/test_view" method="GET">

Instead of:

<form id="postform" action="http://localhost:8000/app/test_view" method="POST">

This is because in action or another place where you add your url, when you develop in local machine you should write url path without domain:
app/test_view instead of http://localhost:8000/app/test_view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM