David J.
David J.

Reputation: 1913

why isn't this django url redirecting?

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

The original HTML should redirect to this page.

learn.html

THIS IS THE LEARN PAGE

Upvotes: 2

Views: 118

Answers (2)

kirbuchi
kirbuchi

Reputation: 2304

Can you go to associate:learn directly?

In your first urls.py

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

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

You should change it to something like:

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

Hope this helps.

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599600

Your associate "index" and "learn" views both have the same URL. You need to have some way of distinguishing between them, otherwise the URL will always be served by the first one, which is index.

Upvotes: 1

Related Questions