Solution 1 :

from django.conf.urls import include, url
from django.contrib import admin
from rtRegRes.views import spartan , units

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^units/$', units),
    url(r'^units/sts/?$', spartan, name='sts'),
]

and

<form method="post" action='sts'>
{% csrf_token %}
 <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="cts_link">cts</button>
</form>

Solution 2 :

Swapping the order of urls should help.

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^units/sts/?$', spartan),
    url(r'^units/$', units),
]

Solution 3 :

What a pity, I wasted my time for nothing (( My code was correct, I just forget to rerun server after changes, every time reloaded page instead and did not see real changes. Anyway, thanks for the responses guys!!

Problem :

I’m trying to create a button in the Django template which will redirect to another URL. But getting error 404 since Django can’t recognize URL path rescribed in the urls.py.

HTML part

<form method="post" action='sts'>
{% csrf_token %}
 <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="cts_link">cts</button>
</form>

urls.py

from django.conf.urls import include, url
from django.contrib import admin
from rtRegRes.views import units
from rtRegRes.views import spartan

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^units/$', units),
    url(r'^units/sts/?$', spartan),
]

views.py

from django.shortcuts import render, redirect, reverse, render_to_response
from .models import rt_reg_res
from django.http import HttpResponse, JsonResponse

def units(request):
    """Return main webpage"""
    return render_to_response('runtime.html')

def spartan(request):
    """Link to the other unit webpages"""
    table = rt_reg_res.objects.all()
    if request.method == 'POST':
        qatables = request.POST.get("cts_link")
        if qatables:
            return render(request, 'cts.html', {'table': table})

Clicking the button following error message appears:
enter image description here

Could somebody point me what is wrong in my code

Thanks

Comments

Comment posted by mats13

Thanks for the response. I have tried that, but it’s not helped… Thanks

By

Leave a Reply

Your email address will not be published. Required fields are marked *