![](/img/trans.png)
[英]I change (rewrite) URL with jquery but when user or I refresh the page, we get 404 not found error
[英]Page not found (404) error happens I did not write the url
我收到一个错误,找不到页面(404)请求方法:GET请求URL: http://localhost:8000/app/test.html?prop=value
。 我写了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>
这部分const test = window.open('test.html?${key}=${value}', "_blank");
导致错误,因为我没有注册url http://localhost:8000/app/test.html?prop=value
。 我只注册http://localhost:8000/app/test_view
。 但是我想将选定的i&value的值发送到test.html,所以我这样写。 我在test.html
这样写
<body>
<h2>RESULT</h2>
<script type="text/javascript">
onload = () => {
document.body.appendChild(
document.createTextNode([...new URLSearchParams(location.search).entries()])
);
}
</script>
</body>
我该如何解决?我该怎么写?
urls.py
是
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^test_view$', views.test_view, name='test_view'),
]
views.py是
def test_view(request):
return render(request, 'test.html')
我的应用程序结构是testapp(父应用程序)-app(子应用程序)-inde.html&test.html&views.py
testapp的urls.py
是
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^app/', include('app.urls')),
]
现在我的代码是
<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>
您注册了http://localhost:8000/app/test_html
但尝试通过http://localhost:8000/app/test.html
链接进入。 您的GET
参数没有任何意义。
您应该使用在urls.py
url。
阅读有关视图和URL的本教程 。
更改表单中的action
属性。 可能看起来像这样:
在您的模板中,您的链接可能如下所示:
<form id="postform" action="{% url 'test_view' %}" method="GET">
要么:
<form id="postform" action="app/test_view" method="GET">
代替:
<form id="postform" action="http://localhost:8000/app/test_view" method="POST">
这是因为在action
或添加URL的其他地方,当您在本地计算机上进行开发时,应编写没有域的URL路径:
app/test_view
而不是http://localhost:8000/app/test_view
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.