Django can't recognize 0,1,2 precisely in url
my_app/views.py
from django.http import HttpResponse,HttpResponseRedirect
articles = {
'index' : "Hello this is a view inside my_app",
'simple' : 'simple views',
'sports' : 'Sports Page',
'finance' : 'Finance Page',
'politics' : "Politics Page"
}
def news_view(request, topic) :
try :
return HttpResponse(articles[topic])
except:
raise Http404("404 GENERIC ERROR")
def num_page_view(request,num):
topics_list = list(articles.keys())
topic = topics_list[num]
return HttpResponseRedirect(topic)
my_app/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<int:num>',views.num_page_view),
path('<str:topic>/',views.news_view),
]
when i type /my_app/0
, django automatically change 0
0/
, so i think Django can't recognize that this page's parameter is pure int
. It continues until I type 0,1,2
But when i type more than 2 (ex. /my_app/3
), it works as i intended
(1. enter /my_app/3
2. redirect to /my_app/finance/
)
I don't know why Django do recognize the int parameter more than 2 but not 0,1,2.
Expected
- enter
/my_app/0
- redirect to
/my_app/index/
But...
- enter
my_app/0
- url is automatically changed to
my_app/0/
- can't redirect to the page so i got an error