Django not recognizing my url, cannot get into the app

63 Views Asked by At

i am very new to Django, just trying out with a url to a view structure basically this:

test\
    project-level files + templates
newyear\
    app files + templates
manage.py

on newyear urls.py

from django.urls import path
from . import views

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

on views.py

from django.shortcuts import render
import datetime

# Create your views here.
def index(request):
    now = datetime.datetime.now()
    return render(request, 'newyear/index.html', {
        "newyear": now.month == 1 and now.day == 1  
    })

on urls.py in test

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('newyear/', include('newyear.urls')),
    path('admin/', admin.site.urls),
]

on settings in test

INSTALLED_APPS = [
    'newyear',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

I have rerun the server but still only recognizes /admin, please help thanks

2

There are 2 best solutions below

0
user16171413 On

You may have to set your app_name in your newyear urls.py. it should look like this:

from django.urls import path
from . import views

app_name = "newyear"   #set this
urlpatterns = [
    path('', views.index, name='index')
]
0
Stefan xyz On

You can reach your website via http://localhost/newyear

This is because of your urlpatterns in urls.py. The include function appends all urls from the given list and appends them unto the original name.

To fix this just remove newyear/ in urls.py

urlpatterns = [
    path('', include('newyear.urls')),
    path('admin/', admin.site.urls), ]