skip to Main Content

I am very new to django and beginning to understand some of the framework however view-route binding is confusing me

There is a persistent issue that when I try to visit any url except for the homepage and /admin I receive a 404, including routes I have declared in my project’s urls.py file

also i am following this mdn tutorial

project urls.py

"""trends URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('articles/', include('articles.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

app named ‘articles’ urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

app named ‘articles’ views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the articles index.")

and here is the 404 page I receive
404 page receieved upon articles/ request

I know this is becoming very long but there is one more odd thing, when I refresh the 404 page, it will toggle between showing me the above screenshot and sometimes show me an old route which is no longer in the urls.py like this
404 returning outdated urls.py list

this is on an nginx server with gunicorn, and restarting the nginx service does not solve the issue

2

Answers


  1. Chosen as BEST ANSWER

    Stumbled upon this SO post which lead me to the idea to restart gunicorn and that solved my problem so try running

    sudo service gunicorn restart
    

    should fix your problems


  2. In your projects urls.py you have defined

    path('articles/', include('articles.urls')),
    

    So by going to YOUR_URL/articles will not give a valid response. Instead try going to YOUR_URL/articles/ or change your path to

    path('articles', include('articles.urls')),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search