'Only homepage url is working in django app. Other urls are not working
My Django app is working on shared Linux hosting but only homepage is working. When I try to access other pages 404 error occurs. I believe urls.py is causing problems. Only empty url path is working others like about, contact, careers are not working. I am using Linux shared hosting by godaddy with cpanel.
urls.py-->
```
from django.contrib import admin
from django.urls import path
from website.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('',welcome, name="home"),
path('about/',aboutus, name="about"),
path('itServ',services, name="serv"),
path('itRec',rec, name="rec"),
path('ManC',management, name="management"),
path('careers',career, name="careers"),
path('contact',contacts, name="contact"),
]```
setting.py-->
```
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'qkepshp^^#7dcucuabdg8@yyo3^2fv9@yyi1a9u7%%4j8*#!v#'
DEBUG = True
ALLOWED_HOSTS = ['northernrecruiters.com','www.northernrecruiters.com']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'ntmc.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ntmc.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = '/home/vbfx44j3hpii/public_html/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/home/vbfx44j3hpii/ntmc/static/'
]
```
views.py-->
```
from django import template
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .forms import FooterForm
from django.http import HttpResponseRedirect
def welcome(request):
# return render(request,"website/index.html")
if request.method=="POST":
form= FooterForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/home')
else:
form= FooterForm()
return render(request,"website/index.html",{'form':form})
def aboutus(request):
return render(request,"website/about.html")
def services(request):
return render(request,"website/services-ITCon.html")
def management(request):
return render(request,"website/services-Manage.html")
def contacts(request):
return render(request,"website/contact.html")
def rec(request):
return render(request,"website/services-ITRec.html")
def career(request):
return render(request,"website/career.html")
```
Solution 1:[1]
from django.contrib import admin
from django.urls import path
from website.views import welcome, aboutus, services, rec, management, career, contacts # like this, no `*`
urlpatterns = [
path('admin/', admin.site.urls),
path('',welcome, name="home"),
path('about/',aboutus, name="about"),
path('itServ/',services, name="serv"),
path('itRec/',rec, name="rec"),
path('ManC/',management, name="management"),
path('careers/',career, name="careers"),
path('contact/',contacts, name="contact"),
]
It looks like you forgot the slashes in the urls. Also I think it goes against best practices to use *
to import all..
'Explicit is better than implicit'. -The Zen of Python
Solution 2:[2]
Problem was with .htaccess file. Thanks for support!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ansh Bajaj |