I am having a hard time configuring django admin to work on elastic beanstalk. I tried everything but it's still not working.
when I click on admin or my flag pages I get Server Error (500).
I have created a folder in my project called management in the root of my project, inside that I created another folder called commands and a file called createsu.py here is the content of the createsu.py
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
class Command(BaseCommand):
def handle(self, *args, **options):
if not User.objects.filter(username="admin").exists():
User.objects.create_superuser("admin", "admin#admin.com", "admin")
self.stdout.write(self.style.SUCCESS('Successfully created new super user'))
in my .config file I added this like of code
03_createsu:
command: "python manage.py createsu"
leader_only: true
after eb deploy and refreshing elastic beanstalk console when I try to go to the webpage I still get the same error. all my other pages are working, static files are working fine except my admin and flag pages because they are connected to the admin. I have been struggling with this for a week now. can someone please help me please.
#I fixed the issue.
#First typo was I forgot to add SITE_ID = 1 in my settings.py because I am using #flatpages.
#Second typo I didn't configure my static files correctly. I fixed it to this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
#Lastly createsu.py should be in the app folder not project folder.
Related
These are the packages that I am using:
Django==3.2
django-storages==1.12.3
I am trying to deploy a django REST API with Vuejs frontend on azure. This is my directory structure for the django API.
I have used djang-storages[azure] to use an azure container blob to store media files. I went through a tutorial to setup the blob connection with django. Some configuration that I did with settings.py are these
Settings.py
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = "my account name"
AZURE_ACCOUNT_KEY="my token"
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
AZURE_LOCATION="media"
AZURE_CONTAINER="media"
STATIC_LOCATION = "static"
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
STATICFILES_STORAGE = 'storages.backends.azure_storage.AzureStorage'
DEFAULT_FILE_STORAGE = 'el.custom_azure.AzureMediaStorage'
AZURE_CONNECTION_TIMEOUT_SECS=100
and my custom_azure.py looks like this:
custom_azure.py
from storages.backends.azure_storage import AzureStorage
class AzureMediaStorage(AzureStorage):
account_name="eltechstorage"
account_key="my token"
azure_container="media"
expiration_specs=None
urls.py
from django.contrib import admin
from django.urls import path,include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path("",include("main.urls"))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
When I am using azure blob container to upload media files it is working perfectly in development environment and when I am testing the API from the deployed url, it is giving me the path of the file as expected on which if i go to, downloads the file for me, everything is working perfect. Below is attached a sample response from the API.
The Problem
But when I use the deployed version of both Django and Vuejs App, and I open the page, where the API returns the object containing link to the file. it displays a 404 error.
according to django doc, the static helper is only workable in debug mode. therefore, in your production environment, if you don't have debug mode enabled, then you static view will not be there. as a temporary measure, you can enable debug by put DEBUG=True in your application settings.
as recommendation by django, you should serve your static files in a dedicate server in production environment.
Hope you're doing well. I've got a django view that needs to check to see if a static file with a known path exists.
I'm currently trying to use os.path.isfile(), but I have a feeling this is the wrong approach.
views.py:
import os
def knighthood_ceremony(warrior):
narration_path = static("polls/narrations/{}/{}.mp3".format(queen_name,warrior.name))
is_to_play_narration = os.path.isfile(narration_path)
settings.py:
STATIC_URL = '/static/'
Structure:
mysite
mysite
settings.py
polls
static
polls
narrations
5
1.mp3
views.py
otherproject
This gives local vars:
narration_path = '/static/polls/narrations/5/1.mp3'
is_to_play_narration = False
But, there is a file at:
C:\Users\{USER}\mysite\polls\static\polls\narrations\5\1.mp3
Why can't os.path.isfile see it? Any advice will be greatly appreciated!
Are you trying to read it via the URL or the STATIC_ROOT? If you are reading from the harddrive. Might be the error there
So I'm trying to run multiple instances of Django on a server for now, one under /dev/, one under /test/, etc..
The web server is setting the correct SCRIPT_NAME setting and I can serve pages, templates, the whole admin panel fine, except for static assets. Static assets are served by Django using WhiteNoise.
The application is supposed to use the value of SCRIPT_NAME as the static URL, ie all static assets are served from the application root.
So far I've tried the following settings against the admin panel:
# SCRIPT_NAME = '/dev/' Set in env
# URL for static assets should be `/dev/`
STATIC_URL = '/' # Browser looks for static assets in `/`, drops script_name
STATIC_URL = None # Browser looks for static assets in `/`, drops script_name
STATIC_URL = `/dev/` # Browser looks for static assets in '/dev/dev/`
I'm wondering if I'm missing a setting here or if the problem might be elsewhere. Going by the docs I understand that STATIC_URL = '/' should work?
Finally got a working config for running my app under /dev/:
# SCRIPT_NAME = '/dev/' set from uwsgi, or use FORCE_SCRIPT_NAME
STATIC_URL = '/dev/'
WHITENOISE_STATIC_PREFIX = '/'
This seems to correctly prepend /dev/ to all static URLs, and makes whitenoise serve static assets from that directory (no subdir).
Not sure if this is the correct approach though?
Thanks #Geotob. this saved me after so many hours of research. As per docs WhiteNoise 6.2 is supposed to handle this automatically. but some how its not working
http://whitenoise.evans.io/en/stable/django.html?highlight=WHITENOISE_STATIC_PREFIX#WHITENOISE_STATIC_PREFIX
I was hit with this issue when i tried to deploy django with nuxt frontend on Digital Ocean's app platform offering. where my django-drf was running on /api. the admin page wouldn't load with static files on /api/admin/....
finally this setting is working for me. below is my snippet if anyone stumbles upon similar situation.
app_route = os.getenv("APP_PLAT_ROUTE", None)
if app_route is not None:
rel_app_route = os.path.relpath(app_route, '/')
FORCE_SCRIPT_NAME = "/{0}".format(rel_app_route)
WHITENOISE_STATIC_PREFIX = '/static/'
STATIC_URL = "/{0}/static/".format(rel_app_route)
I am trying to override customer app in django-oscar. For that I have created customer app in my apps folder in project. While I run that project I encountered an error in django 1.7.4 as below:
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: customer
I went through the doc in django https://docs.djangoproject.com/en/1.7/ref/applications/#django.apps.AppConfig, But its not working out. So Is there any other way to extend any django-oscar's app and modify the code as per requirements.
This is my customer app's views.py:
from oscar.apps.customer.views import ProfileView as CoreProfileView
class ProfileView(CoreProfileView):
template_name = 'new_account.html'
and below is project's settings.py code snippet:
INSTALLED_APPS = [
'apps.customer',
]
Thanks in advance.
Run this command to overide apps from django oscar
./manage.py oscar_fork_app appname yourprojectname
yourprojectname-Your folder path to where the app should be created
Once you run this command a new app will be created with overided models,admin files.now add the app path inside
get_core_apps(['yourproject.order']) in settings.py file.
For more information please refer
http://django-oscar.readthedocs.org/en/latest/topics/customisation.html
I am deploying my django project, i set up the project on server and once i go to url, i end up seeing this error message.
since the error message is huge, i am adding screenshot here.
http://imagebin.org/279726
I created 500.html and 400.html.
this is my template settings in settings.py on prod server
TEMPLATE_DIRS = (
"templates",
os.path.join(PROJECT_PATH, "templates"),
)
what am i doing wrong?