I created a superuser for my site, but when I started the server and tried to connect to the localhost:8000/admin page, I got an ERR_CONNECTION_REFUSED (site cannot be reached) error, and the server stopped running.
urls.py :
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('blog.urls'))
]
settings.py :
# Application definition
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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',
]
When I start the server, I get the below message:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
May 17, 2020 - 18:25:44
Django version 3.0.1, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
same happened to me. I realized that i defined the path with / but i was writing localhost:8000/admin
path('admin/', admin.site.urls)
I should type admin/ localhost:8000/admin/
Check if the firewall is causing any problem. (If only admin page is causing this problem, then firewall is not causing any probelm.)
Make sure that you have made necessary changes in urls.py and views.py.
Project's urls.py should include
path('admin/', admin.site.urls), in urlpatterns list.
settings.py should include
INSTALLED_APPS = ['django.contrib.admin',
...#more apps
]`
Let me know if this is helpful.
I was facing the same problem .My vpn was causing the problem .Go to chrome menu >setting>advanced settings>here my vpn was controlling the proxy settings and just disabling it fixed the issue .All the best
Try cleaning browser cache and then refreshing.
Additionally, in case you have created a custom User model, make sure you
Register your custom user model in the admin.py of the app where you defined your custom user model (see https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_site#registering_models)
In the settings.py, define a variable AUTH_USER_MODEL = '<app-name>.<custom-user-model-name>'
you forgot to runserver! python manage.py runserver
Related
I am attempting Django Channels for the first time. I am following this tutorial - https://www.youtube.com/watch?v=cw8-KFVXpTE&t=21s - which basically explains Channels basics. I installed Channels in my virual environment using pip install channels and it installed the latest version, which is 4.0.0. I have laid out the basic setup. But when I run python manage.py runserver, I get -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
when I shoul be getting this -
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 10, 2023 - 02:37:40
Django version 4.1.3, using settings 'BrokerBackEnd.settings'
Starting ASGI/Channels version 4.0.0 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
I cannot figure out what I might be doing wrong here and I cannot find any relevant solution anywhere. I would really appreciate any suggestions anyone might have.
settings.py
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
import Orders.routing as route
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BrokerBackEnd.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter(
route.websocket_urlpatterns
)
})
consumer.py
class OrderConsumer(WebsocketConsumer):
def connect(self):
self.accept()
def receive(self, text_data=None, bytes_data=None):
self.send(text_data="Message = " + text_data)
def disconnect(self, code):
return super().disconnect(code)
routing.py
websocket_urlpatterns = [
re_path(r"socket/order/", OrderConsumer.as_asgi())
]
As of django-channels 4.0.0 the daphne server was decoupled from the rest of channels. Now, you need to include daphne in your settings.py INSTALLED_APPS if you wish to use it. Include this at the top of your installed apps. The tutorial you are looking at uses an older version of channels.
INSTALLED_APPS = [
'daphne',
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'Orders',
]
ASGI_APPLICATION = "BrokerBackEnd.asgi.application"
Also, make sure daphne is actually installed:
pip install -U channels["daphne"]
I have used swagger to document my APIs on the Django rest framework, and deploy it on Pythonanywhere.
but the documentation URL doesn't have any response.
it is my urls.py code for swagger:
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="BoardGame API",
default_version='v1',
description="Test description",
terms_of_service="http://****.pythonanywhere.com/",
contact=openapi.Contact(email="contact#boardgame.local"),
license=openapi.License(name="TEST License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
...
path('', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
]
and my settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'drf_yasg',
'rest_framework_swagger',
'rest_framework_simplejwt',
'django_extensions',
...
]
Its because your static files has not been loaded on server try to collectstatic files and check in inspect of page whether css is loading or not
You have to learn about static in Django
When you apply it all UI related files will be created in a static folder
Then swagger UI will work on pythonanywhere
It's seems that static file is not loaded on live server:
step 1:
LOCAL= False
step 2:
python manage.py collectstatic
step 3:
Reset server
nginx: sudo systemctl restart nginx
gunicorn: sudo systemctl restart gunicorn
I don't know if I am explaining my problem correctly but I can clarify with the following picture of my settings.py file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = False
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['sheryar-portfolio.herokuapp.com', '127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
'calculator',
'amino'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
]
Now here in ALLOWED_HOSTS, I have declared two hosts.
The Heroku one is running perfectly whereas I am having some issues in the local one.
It seems like that my static folder is not being located by django as I can only see the HTML of my website in local server.
In short, I cannot see the CSS being applied to my website when I am including the local server in ALLOWED_HOST
Im following a guide on youtube to work with django. Unfortunately this guide is made for pre 2.0 django.
So the challenge is to create a app called "posts" that can be accessed by localhost/posts.
After creating the folder 'posts' and adding it to settings.py MIDDLEWARE like this:
MIDDLEWARE = [
'posts',
'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',
]
python manage.py runserver fails
When commenting 'posts' out runserver succedes.
The problem is that Im so new to all of this that I don't even know what to search for.
try adding 'post' in INSTALLED_APPS inside settings.py like this:
INSTALLED_APPS = [
'post',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
and delete it from middleware.
You have to "install" all the apps you make
I want to use django.contrib.auth.middleware.RemoteUserMiddleware for authentication as outlined here:
https://docs.djangoproject.com/en/dev/howto/auth-remote-user/
Question is, how can I test this in a dev environment where there is no Apache? i.e. can I set REMOTE_USER somehow in local settings?
EDIT (adding settings)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.RemoteUserBackend',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.RemoteUserMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
...
)
Then I have this in my local_Settings:
os.environ['REMOTE_USER'] = "mmatyas"
I also tried the 'HTTP_REMOTE_USER' variant. Thanks!
The variable has to be set in the environment in which you start the dev server. usually it's just:
REMOTE_USER=myuser ./manage.py runserver
You can write some WSGI middleware and set it there, and then make sure the wsgi middleware wraps your django app in the wsgi stack. lots of tutorials around on writing wsgi middleware, it'll be a short one page of code. Easy-peasy-lemon-squeezy!
In your dev environment, you can set an environment variable in the same command prompt you use to start up your dev server.
Something like export REMOTE_USER="duncan" if on a Unixy machine.
You can also do this by editing your manage.py and setting os.environ['REMOTE_USER'] = "duncan"