drichardsxmv
drichardsxmv

Reputation: 21

Unable to Access Other Apps in Django

I am writing a beginner Django app. I am currently able to the first app that I created. However, I am not able to figure out how to access the other two apps.

swiss (project)
  |_ app_picker (default app)
  |_ eft
  |_ servermon

I am currently able to access the app_picker by typing 127.0.0.1:8000/app_picker/. But when I type in 127.0.0.1:8000/eft or 172.0.0.1:8000/servermon the page is not found. What am I failing to understand with my Django config?

Installed Apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_picker',
    'etf',
    'servermon',
]

Project URLS

from django.contrib import admin
from django.urls import path
from django.views.generic import RedirectView
from django.urls import include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app_picker/', include('app_picker.urls')),
    path('etf/', include('etf.urls')),
    path('servermon/', include('servermon.urls')),
    path('', RedirectView.as_view(url='app_picker/', permanent=True)),
]

#Add Django site authentication urls (for login, logout, password management)
urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]

app_picker URLs

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from . import views

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

eft URLs

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from . import views

urlpatterns = [
    path('eft/', views.base, name='eft_base'),
]

eft Views

from django.shortcuts import render

# Create your views here.
def base(request):
    return render(request, 'eft_base.html')

servermon URLs

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from . import views

urlpatterns = [
    path('servermon/', views.base, name='servermon_base'),
]

servermon Views

from django.shortcuts import render

# Create your views here.
def base(request):
    return render(request, 'servermon_base.html')

Upvotes: 0

Views: 380

Answers (3)

Chris
Chris

Reputation: 1

Did you include 'etf' and 'servermon' url in project urls.py file?

path('admin/', admin.site.urls),
path('', include('tvapp.urls')),
path('', include('wifiapp.urls')),

Upvotes: 0

Dipen Dadhaniya
Dipen Dadhaniya

Reputation: 4630

In etf.urls.py change:

path('eft/', views.base, name='eft_base'),

To:

path('', views.base, name='eft_base'),

And then access:

127.0.0.1:8000/eft/    

Similarly, in servermon.urls.py change:

path('servermon/', views.base, name='servermon_base'),

To:

path('', views.base, name='servermon_base'),

And then access:

127.0.0.1:8000/servermon/

Without changing these files, if you want to access, then you need to use:

127.0.0.1:8000/eft/eft/

Here from the url eft/eft/:

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

Matches the first eft/ and then search continues with the remaining part eft/ in etf.urls.py:

path('eft/', views.base, name='eft_base'),

Matches the remaining part eft/ and the views.base is used to process the request.


Similarly for the other one use:

172.0.0.1:8000/servermon/servermon/

You might want to read: URL dispatcher and django.urls functions for use in URLconfs.

Upvotes: 1

Toan Quoc Ho
Toan Quoc Ho

Reputation: 3378

It's because your path definition is not the same as your expected result.

eft Urls should be like this:

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

and servermon Urls:

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

This because when you access 172.0.0.1:8000/servermon/, django will go to ROOT_URLS (Project Urls) to matching the path. Found /servermon/, then it will extract this part in the url and then go next to the include('servermon.urls'). After extracting the matched part, now it only have '' so your servermon path should be like above.

Upvotes: 0

Related Questions