[英]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
. 我收到一个错误,找不到页面(404)请求方法:GET请求URL:
http://localhost:8000/app/test.html?prop=value
。 I wrote index.html
like 我写了
index.html
像
<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");
这部分
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
. 导致错误,因为我没有注册url
http://localhost:8000/app/test.html?prop=value
。 I only register http://localhost:8000/app/test_view
. 我只注册
http://localhost:8000/app/test_view
。 But I wanna send selected i & value's values to test.html,so I wrote this like. 但是我想将选定的i&value的值发送到test.html,所以我这样写。 I wrote in
test.html
like 我在
test.html
这样写
<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 urls.py
是
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^test_view$', views.test_view, name='test_view'),
]
views.py is views.py是
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(父应用程序)-app(子应用程序)-inde.html&test.html&views.py
testapp's urls.py
is testapp的
urls.py
是
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. 您注册了
http://localhost:8000/app/test_html
但尝试通过http://localhost:8000/app/test.html
链接进入。 Your GET
parameters don't make a sense. 您的
GET
参数没有任何意义。
You should use url, that you registred in urls.py
. 您应该使用在
urls.py
url。
Read this tutorial about views and urls. 阅读有关视图和URL的本教程 。
Change action
attribute in your form. 更改表单中的
action
属性。 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: 这是因为在
action
或添加URL的其他地方,当您在本地计算机上进行开发时,应编写没有域的URL路径:
app/test_view
instead of http://localhost:8000/app/test_view
app/test_view
而不是http://localhost:8000/app/test_view
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.