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..
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..
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.
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). 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..
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
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
.
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:
url(r'^cholera/', include('SeqData.urls', namespace='seqdata'))
and url in action attribute as {% 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:
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.
This can be then even further improved by moving val1 <= val2
to form validation so the form will not validate if val1 <= val2
. Relevant part of Django documentation is in subchapter Cleaning and validating fields that depend on each other . Your InForm
should then look something like this:
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 }}
. You can check documentation Rendering form error messages for more info. Adjusted input_form()
will be something like this:
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
<form action="{% url result_page%}" method="post">
And most importantly restart the apache2 to load and implement the changes..
and it worked for me..
thanks
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.