1

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

Liam O'Toole
  • 167
  • 3
  • 13
  • Please check your project's `settings.py` to see if you have defined the app `articles` in `INSTALLED_APPS`. You can refer to this document https://docs.djangoproject.com/en/3.2/ref/applications/#configuring-applications – Lavande Jun 21 '21 at 10:50
  • @Lavande I have – Liam O'Toole Jun 21 '21 at 10:50
  • The configurations provided here look good to me. Maybe need to review the Django directory structure in your project. Not sure if this thread would help: https://stackoverflow.com/questions/34220959/the-current-url-app-didnt-match-any-of-these – Lavande Jun 21 '21 at 11:08
  • My project and app structures are correct, according to django docs and that SO post – Liam O'Toole Jun 21 '21 at 11:12

2 Answers2

0

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')),
Hussain Pettiwala
  • 1,406
  • 8
  • 16
0

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

Liam O'Toole
  • 167
  • 3
  • 13