如何解决Django中奇怪的URL错误?

2024-09-26 18:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的表格。。你知道吗

{% 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})

项目网址.py这样地。。你知道吗

urlpatterns = [

    url(r'^cholera/', include('table.urls'), name='cholera'),
    url(r'^cholera/', include('DATAPLO.urls')),
    url(r'^cholera/', include('SeqData.urls')),
]

应用网址.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年,以及下拉示例“india”中的国家名称,当我单击submit按钮时返回相关数据,它成功返回一个url,如给定的bellow和数据表。你知道吗

127.0.0.1:8000/cholera/SeqData/result_page

当我把这段代码传输到我的VPS并在apache2服务器上运行时,除了submit按钮返回一个url,就像下面给出的那样(显示的IP是一个虚拟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">

和项目网址.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

甚至连形式都没有打开。。你知道吗


Tags: namemargindivformurlincludestylerequest
2条回答

这是您在表单操作中的url:

/cholera/SeqData/result_page/

这是你的网址网址.py你知道吗

url(r'^cholera/', include('SeqData.urls')),
url(r'^$', views.result_page, name='result_page'),

也就是说

/cholera/

如您所见,/cholera/SeqData/result_page/不在您的urls.py中。你知道吗

按照@mohammed qudah的建议,将表单的action属性中定义的url更改为{% url 'result_page' %},或者更好地为SeqData定义名称空间,并使用如下名称空间定义url:

 url(r'^cholera/', include('SeqData.urls', namespace='seqdata'))

和url-in-action属性为{% url 'seqdata:result_page' %}。你知道吗

总的来说,你在那里做的设计有点糟糕。^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(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})

绞尽脑汁之后我找到了答案。。你知道吗

像这样更改了URL。。你知道吗

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的建议,我在表单.html你知道吗

<form action="{% url result_page%}" method="post">

最重要的是重新启动apache2以加载和实现更改。。你知道吗

这对我很有效。。你知道吗

谢谢

相关问题 更多 >

    热门问题