manage.py runserver fails after putting custom app in middleware - python

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

Related

I got error when I turn DEBUG true on settings file

I got Error500 when I turn DEBUG True on settings.py but everything is okay when DEBUG is False. python manage.py collectstatic is okay too.
Here my settings.py. I will appreciated your help. I use Django 2.2 version.
Django settings
"""
Django settings for samamarche project.
Generated by 'django-admin startproject' using Django 2.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import dj_database_url
import os
import django_heroku
if os.environ.get('ENV') == 'PRODUCTION':
# ...
db_from_env = dj_database_url.config(conn_max_age=500)
#DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
DATABASES['default'].update(db_from_env)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.environ.get('SECRET_KEY', 'xxxxxxxxxxxxxxx')
DEBUG = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'phonenumber_field',
'marche',
'cart',
'commandes',
'payment',
'coupons',
#'accounts',
'crispy_forms',
'users',
]
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'
]
i think adding ALLOWED_HOSTS = ['*'] will solve the problem

How do I allow my django website to be hosted by a local server (127.0.0.1) and a Heruko server

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

Cannot connect to localhost:8000/admin page, Django

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

How to use django-storages for media storage on dropbox?

I am using django-storages to serve media files on Dropbox. But I cannot get it working (media files are still stored in local server).
I've installed dropbox and django-storages, and created an app with permission type:app folder, then added related settings.
Here's my settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
DEBUG = False
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_filters',
'website',
'storages',
)
# for dropbox
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'my_generated_token_from_dropbox'
DROPBOX_ROOT_PATH = 'media'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
To be able to upload the media files to Dropbox (and reading them) what has to be changed in the settings code?
Ok, found the problem! The settings are correct, the problem was that for some reason I was using a custom storage for my FileField! All I had to do was removing the storage=...:
file = models.FileField(upload_to=some_path, storage=CustomStorage())
to
file = models.FileField(upload_to=some_path)

Django message error

The following error is being thrown by my Django code on my VPS when I try to use messages:
You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
Exception Type: MessageFailure
Exception Value:
You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
I do have all the appropriate settings in my settings.py file. My VPS is running Django 1.9.6. The same error is not thrown when I run the code locally on my laptop. On my laptop, I have Django 1.10.3 running. Are there any differences in the treatment of messages between Django 1.9.6 and 1.10.3 that would cause this to happen? It seems that on Django 1.9.6 my settings in settings.py are being ignored.
Relevant code in settings.py is as follows:
INSTALLED_APPS = [
'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.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
Any help would be greatly appreciated. Thanks.
INSTALLED_APPS = [
'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',
]
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',
],
},
},
]
follow the code with the order of each

Categories

Resources