2

I ran into a issue with my URL patterns in Django 2.2.5. I cannot navigate to the proper URL patterns for my apps. When I run my server I able to navigate to the base url ":8000/" as defined in APP1/url.py and views.py.

When I enter :8000/signin I get the following error:

Using the URLconf defined in modmagazine_base.urls, Django tried these URL patterns, in this order:

admin/
[name='homepage']
signup/
signin/
coffeetable/
crafttable/
The current path, signin, didn't match any of these.

However, if I add a "/" to the URL (:8000/signin/) I receive this error:

Using the URLconf defined in modmagazine_base.urls, Django tried these URL patterns, in this order:

admin/
[name='homepage']
signup/
signin/ signup/ [name='signup']
signin/ signin/ [name='signin']
signin/ coffeetable/ [name='coffeetable']
coffeetable/
crafttable/
The current path, signin/, didn't match any of these.

The only way I can get to the views I defined is to duplicate the URL (:8000/signin/signin). If you look at my project files below this is not how I defined my URL patterns. I'm not sure where my error is. If you can help me thank you in advance!

My project structure is as follows:

  • PROJECT
    • BASE
    • APP1
    • APP2
    • APP3
  • manage.py

BASE/SETTINGS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'BASE',
    'APP1',
    'APP2',
    'APP3',
]

BASE/URLS.PY

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('APP1.urls')),
    path('signup/', include('APP2.urls')),
    path('signin/', include('APP2.urls')),
    path('coffeetable/', include('APP2.urls')),
    path('crafttable/', include('APP3.urls')),

]

APP1/URLS.PY and APP1/VIEWS.PY

#URLS.PY
#----------
from django.contrib import admin
from django.urls import path
from . import views

app_name = "APP1"
urlpatterns = [
    path('', views.homepage, name='homepage'),
]

#VIEWS.PY
#----------
from django.shortcuts import render
from django.http import HttpResponse

def homepage(request):
    return HttpResponse("You made it home!")

APP2/URLS.PY and APP2/VIEWS.PY

#URLS.PY
#----------
from django.contrib import admin
from django.urls import path
from . import views

app_name = "APP2"
urlpatterns = [
    path('signup/', views.signup, name='signup'),
    path('signin/', views.signin, name='signin'),
    path('coffeetable/', views.coffeetable, name='coffeetable'),
]

#VIEWS.PY
#----------
from django.shortcuts import render
from django.http import HttpResponse

def signup(request):
    return HttpResponse("You made it to the Sign Up page!")

def signin(request):
    return HttpResponse("You made it to the Sign In page!")

def coffeetable(request):
    return HttpResponse("You made it to the Coffee Table!") 

APP3/URLS.PY and APP3/VIEWS.PY

#URLS.PY
#----------
from django.contrib import admin
from django.urls import path
from . import views

app_name = "APP3"
urlpatterns = [
    path('crafttable/', views.crafttable, name='crafttable'),
]

#VIEWS.PY
#----------
from django.shortcuts import render
from django.http import HttpResponse

def crafttable(request):
    return HttpResponse("You made it to the Craft Table!")

1 Answers1

0

You include the paths from App2.urls like this:

path('signup/', include('APP2.urls')),

The path in the urls.py of APP2 looks like this:

path('signup/', views.signin, name='signin'),

That means you include /signup under /signup

Change the path in your base urls.py to this and it should work:

urlpatterns = [
    path('', include('APP2.urls')),
]

You also don't need to define /signup and /signin in your base urls.py. The include function includes all the patterns you have defined in the urls.py you include in this place.

If you include the urls like I desribed above all the urls you defined will be attatched to the base url.

example.com/signup
example.com/signin
example.com/coffeetable

The urls are include at the place you include them:

urlpatterns = [
    path('accounts', include('APP2.urls')),
]

This gives you urls like this: example.com/accounts/signup

LonnyT
  • 590
  • 1
  • 8
  • 21