我有这样的形式..
{% 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 %}
像这样的观点..
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})
像这样投射URL.py ..
urlpatterns = [
url(r'^cholera/', include('table.urls'), name='cholera'),
url(r'^cholera/', include('DATAPLO.urls')),
url(r'^cholera/', include('SeqData.urls')),
]
像这样的应用程序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'),
]
当我在本地服务器上运行此代码并以表格形式提交年份范围时,此代码运行完全正常,例如2001年至2016年,国家名称来自下拉列表示例&#34; india&#34;,返回相关数据时,我点击提交按钮,它会成功返回一个像给定波纹管和数据表的网址。
127.0.0.1:8000/cholera/SeqData/result_page
当我将此代码传输到我的VPS并在apache2服务器上运行时,每件事情都很好,除了,提交按钮返回一个像给定波纹管的URL(显示的IP是虚拟的)。以及如下所示的错误页面。
92.166.167.63/cholera/SeqData/result_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.
我怎么能解决这个奇怪的问题请帮忙.. 谢谢
更新 我已经改变了这样的形式动作..
<form action="{% url 'seqdata:result_page' %}" method="post">
和项目urls.py这样..
url(r'^cholera/', include('table.urls'), name='cholera'),
url(r'^cholera/', include('DATAPLO.urls')),
url(r'^cholera/', include('SeqData.urls'), namespace='seqdata')
显示错误..
NoReverseMatch at /cholera/SeqData/
u'seqdata' is not a registered namespace
甚至形式都没有打开..
答案 0 :(得分:1)
这是来自表单操作的网址:
/cholera/SeqData/result_page/
这是来自urls.py的网址
url(r'^cholera/', include('SeqData.urls')),
url(r'^$', views.result_page, name='result_page'),
转换为
/cholera/
您可以看到/cholera/SeqData/result_page/
中没有urls.py
。
将表单的action属性中定义的url更改为{% url 'result_page' %}
@ mohammed-qudah建议或更好,为SeqData定义名称空间并使用名称空间定义url:
url(r'^cholera/', include('SeqData.urls', namespace='seqdata'))
和url in action属性为{% url 'seqdata:result_page' %}
。
总的来说,你在那里做的是一种糟糕的设计。 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})
...并从表单中删除属性操作(action="/cholera/SeqData/result_page/"
)。
然后可以通过将val1 <= val2
移动到表单验证来进一步改进,以便表单不会验证val1 <= val2
。 Django文档的相关部分在章节Cleaning and validating fields that depend on each other中。您的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 ...")
您可以通过调用{{ form.non_field_errors }}
在表单中显示此错误消息。您可以查看文档Rendering form error messages以获取更多信息。调整后的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})
答案 1 :(得分:0)
经过多次扼杀后,我找到了答案..
更改了这样的网址..
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'),
]
并根据 @Borut 建议我在form.html
中执行了此操作<form action="{% url result_page%}" method="post">
最重要的是重新启动apache2以加载和实现更改。
它对我有用..
谢谢