[英]How to solve strange URL ERROR in Django?
I have form like this.. 我有这样的形式。
{% extends 'DATAPLO/base.html' %}
{% load staticfiles %}
{% block content %}
<form action="/cholera/SeqData/result_page/" method="post">
<div style=" color: #1f5a09; margin: auto; margin-top: 10px; height: 15%; border: 1px solid green; box-sizing: border-box;background: #9fc36a; width: 100%">
<div style= "margin-left: 15px;">
<p style="color: #000000 "; > <b>Enter year range to explore seqence data country wise</b> </p>
</div>
{% csrf_token %}
<div style= "margin-left: 5px; margin-right: 50px;margin-top: 2px">
<b>Start year:</b>
{{ form.start_year }}
</div>
<div style= "margin-left: 13px; margin-right: 54px;margin-top: 2px">
<b> End year:</b>
{{form.end_year }}
</div>
<div style= "margin-left: 17px; margin-right: 50px;margin-top: 2px">
<b>Country:</b>
{{form.country }}
</div>
<div style= "margin-left: 75.5px; margin-right: 50px;margin-top: 5px">
<input type="submit" value="Submit"/>
</div>
</div>
</form>
{% endblock %}
Views like this.. 这样的看法..
def input_form(request):
if request.method == 'POST':
form = InForm(request.POST)
else:
form = InForm()
return render(request, 'SeqData/in_form.html', {'form': form})
def result_page(request):
form = InForm(request.POST)
if form.is_valid():
val1 = form.cleaned_data['start_year']
val2 = form.cleaned_data['end_year']
val3 = form.cleaned_data['country']
if val1 <= val2:
IDs = SequenceDetails.objects.all().filter(country=val3).filter(year__range =[val1,val2])
return render(request,'SeqData/slider.html',{'IDs':IDs})
else:
return render(request,'SeqData/ERROR.html',{'val1':val1, 'val2':val2})
project URL.py like this.. 像这样的项目URL.py。
urlpatterns = [
url(r'^cholera/', include('table.urls'), name='cholera'),
url(r'^cholera/', include('DATAPLO.urls')),
url(r'^cholera/', include('SeqData.urls')),
]
Application URL.py like this.. 像这样的应用程序URL.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^SeqData/$', views.input_form, name='input_form'),
url(r'', views.result_page, name='result_page'),
]
This code runs perfectly fine when I run this code on local server and submitting year range in form, for example 2001 to 2016 and country name from a drop down example "india", return related data when I clicked submit button and it return a url like given bellow and data table successfully. 当我在本地服务器上运行此代码并以格式提交年份范围(例如2001年至2016年)以及下拉示例“印度”中的国家/地区名称时,此代码运行得非常好,当我单击“提交”按钮并返回url时返回相关数据像给定的波纹管和数据表成功。
127.0.0.1:8000/cholera/SeqData/result_page
When I transfer this code to my VPS and run on apache2 sever every thing works great except, submit button returns a url like given bellow (shown IP is a dummy). 当我将此代码传输到我的VPS并在apache2服务器上运行时,除正常工作外,其他所有功能都很好,提交按钮返回的URL类似于给定的波纹管(显示的IP是虚拟的)。 and an error page like given bellow.
并出现如下错误页面。
92.166.167.63/cholera/SeqData/result_page
Error page.. 错误页面
Page not found (404)
Request Method: POST
Request URL: http://93.188.167.63/cholera/SeqData/result_page/
Using the URLconf defined in trytable.urls, Django tried these URL patterns, in this order:
^cholera/ ^$ [name='table_list']
^cholera/ ^contact/$ [name='contact']
^cholera/ ^about/$ [name='about']
^cholera/ ^database/$ [name='database']
^cholera/ ^DATAPLO/$ [name='Country_name_list']
^cholera/ ^DATAPLO/(?P<pk>\d+)/$ [name='County_Details']
^cholera/ ^fasta/$ [name='fasta_list']
^cholera/ ^fasta/(?P<pk>\d+)/$ [name='fasta_detail']
^cholera/ ^test_m/$ [name='Student_list']
^cholera/ ^test_m/(?P<pk>\d)/$ [name='Student_info']
^cholera/ ^SeqData/$ [name='input_form']
^cholera/ ^SeqData/$ [name='result']
^cholera/ ^upload_vew/ [name='upload_vew']
^cholera/ ^DataUpload/ [name='DataUpload']
The current URL, cholera/SeqData/result_page/, didn't match any of these.
How could I solve this strange problem please help.. thanks 我该如何解决这个奇怪的问题,请帮忙..谢谢
UPDATE I have change form action like this.. 更新我有这样的改变形式的行动..
<form action="{% url 'seqdata:result_page' %}" method="post">
and project urls.py like this.. 和项目urls.py这样。
url(r'^cholera/', include('table.urls'), name='cholera'),
url(r'^cholera/', include('DATAPLO.urls')),
url(r'^cholera/', include('SeqData.urls'), namespace='seqdata')
its showing error.. 它显示错误..
NoReverseMatch at /cholera/SeqData/
u'seqdata' is not a registered namespace
and even form is not opening.. 甚至形式还没有开放。
This is you url from form action: 这是您来自表单操作的网址:
/cholera/SeqData/result_page/
and this is your url from urls.py 这是您来自urls.py的网址
url(r'^cholera/', include('SeqData.urls')),
url(r'^$', views.result_page, name='result_page'),
which translates to 转化为
/cholera/
As you can see /cholera/SeqData/result_page/
is not in your urls.py
. 如您所见,
/cholera/SeqData/result_page/
不在urls.py
。
Change url defined in your form's action attribute to {% url 'result_page' %}
as @mohammed-qudah suggested or better yet, define namespace for SeqData and define url with namespace like: 按照@ mohammed-qudah的建议或更好的方法,将在表单的action属性中定义的url更改为
{% url 'result_page' %}
,为SeqData定义名称空间,并使用以下名称空间定义url:
url(r'^cholera/', include('SeqData.urls', namespace='seqdata'))
and url in action attribute as {% url 'seqdata:result_page' %}
. 并将url中的action属性设为
{% url 'seqdata:result_page' %}
。
Overall, what you do there is kind of a bad design. 总体而言,您在这里所做的事情是一种糟糕的设计。
if
in input_form()
doesn't do anything and You should consider joining both functions into one like this: if
在input_form()
中没有执行任何操作,则应考虑将这两个函数合并为一个:
def input_form(request):
if request.method == 'POST':
form = InForm(request.POST)
if form.is_valid():
val1 = form.cleaned_data['start_year']
val2 = form.cleaned_data['end_year']
val3 = form.cleaned_data['country']
if val1 <= val2:
IDs = SequenceDetails.objects.filter(country=val3).filter(year__range =[val1,val2])
return render(request,'SeqData/slider.html',{'IDs':IDs})
else:
return render(request,'SeqData/ERROR.html',{'val1':val1, 'val2':val2})
else:
form = InForm()
return render(request, 'SeqData/in_form.html', {'form': form})
... and delete attribute action( action="/cholera/SeqData/result_page/"
) from form. ...,然后从表单中删除属性action(
action="/cholera/SeqData/result_page/"
)。
This can be then even further improved by moving val1 <= val2
to form validation so the form will not validate if val1 <= val2
. 然后,可以通过移动
val1 <= val2
进行表单验证来进一步改善,以便如果val1 <= val2
则表单将不进行验证。 Relevant part of Django documentation is in subchapter Cleaning and validating fields that depend on each other . Django文档的相关部分在子章节的清洗和验证字段中,它们相互依赖 。 Your
InForm
should then look something like this: 然后,您的
InForm
应该看起来像这样:
class InForm(forms.ModelForm):
# Everything as before.
...
def clean(self):
cleaned_data = super().clean()
val1 = cleaned_data.get("val1")
val2 = cleaned_data.get("val2")
if val1 <= val2:
raise forms.ValidationError("... this is an error message ...")
You can display this error message in form by calling {{ form.non_field_errors }}
. 您可以通过调用
{{ form.non_field_errors }}
以表单形式显示此错误消息。 You can check documentation Rendering form error messages for more info. 您可以查看文档“ 渲染表单”错误消息以获取更多信息。 Adjusted
input_form()
will be something like this: 调整后的
input_form()
将如下所示:
def input_form(request):
if request.method == 'POST':
form = InForm(request.POST)
if form.is_valid():
val1 = form.cleaned_data['start_year']
val2 = form.cleaned_data['end_year']
val3 = form.cleaned_data['country']
IDs = SequenceDetails.objects.filter(country=val3).filter(year__range =[val1,val2])
return render(request,'SeqData/slider.html',{'IDs':IDs})
else:
print(form.errors)
else:
form = InForm()
return render(request, 'SeqData/in_form.html', {'form': form})
After strungling a lot I find the answer.. 挣扎了很多之后,我找到了答案。
Changed urls like this.. 这样更改网址。
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^SeqData/$', views.input_form, name='input_form'),
url(r'^SeqData/result_page$', views.result_page, name='result_page'),
]
and as per @Borut suggestion I did this in form.html 根据@Borut的建议,我在form.html中进行了此操作
<form action="{% url result_page%}" method="post">
And most importantly restart the apache2 to load and implement the changes.. 最重要的是,重新启动apache2以加载并实施更改。
and it worked for me.. 它为我工作。
thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.