为什么这个 django url 没有重定向? [英] why isn't this django url redirecting?

查看:39
本文介绍了为什么这个 django url 没有重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从以下表单获取帖子数据后,页面应重定向到associate:learn",如操作所示.但是,它只停留在单选按钮页面上.我怀疑我犯了一个初学者的错误,但在重新阅读教程后,我不确定发生了什么.

After getting post data from the following form, the page should redirect to 'associate:learn' as shown in the action. However, it just stays on the radio button page. I suspect I'm making a beginner's error but after rereading the tutorial, I'm not sure what's going on.

index.html

Choose a dataset 

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
    <input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
    <label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', "associate.views.index", name='index'),
    url(r'^$', "associate.views.learn", name='learn'),
)

urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^images/', include('images_app.urls', namespace="images_app")),
    url(r'^associate/', include('associate.urls', namespace="associate")),
    url(r'^admin/', include(admin.site.urls)),
)

views.py

def index(request):

    images = Image.objects.all()
    datasets = []
    for i in images:

        if i.rank() >= 3:

            datasets.append(i)

    return render(request, 'associate/index.html', {'datasets':datasets})

原始 HTML 应重定向到此页面.

The original HTML should redirect to this page.

learn.html

THIS IS THE LEARN PAGE

推荐答案

可以直接去associate:learn吗?

在你的第一个 urls.py

In your first urls.py

urlpatterns = patterns('',
    url(r'^$', "associate.views.index", name='index'),
    url(r'^$', "associate.views.learn", name='learn'),
)

网址将始终匹配associate.views.index",因为它出现在associate.views.learn"之前,并且它们都具有相同的网址.

The url will always match "associate.views.index" since it appears before "associate.views.learn" and they both have the same url.

您应该将其更改为:

urlpatterns = patterns('',
    url(r'^$', "associate.views.index", name='index'),
    url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)

希望这会有所帮助.

这篇关于为什么这个 django url 没有重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆