django listing files instead of showing home page - python

I'm not able to figure out what's going wrong. I'm using pycharm and bitnami django stack for developing my first web application.
Here is my directory structure:
project name: myapp
location: C:\Bitnami\djangostack-1.7.8-0\apache2\htdocs\myapp
Directory structure:
myapp
manage.py
app
admin.py
models.py
settings.py
tests.py
urls.py
views.py
wsgi.py
migrations
templates
home.html
my settings.py has following data:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
)
ROOT_URLCONF = 'app.urls'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
my urls.py has following data:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'myapp/', views.homepage, name='home'),
]
my views.py has following data:
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world!")
Now when I try to run:
http://localhost/myapp
It simply displays the list of files in the myapp directory
I'm not able to find why it is not executing from urls.py

If you want to get your app up and running with Apache, you will have to enable reverse-proxying.
To do this, you can try adding the following lines to /opt/bitnami/apache2/conf/httpd.conf, supposing that your application myapp is running at port 8000:
ProxyPass /myapp http://localhost:8000/myapp
ProxyPassReverse /myapp http://localhost:8000/myapp
To make sure that the mod_proxy module is enabled, please find the following lines and uncomment them:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
Finally, after saving the changes you can proceed to restart Apache:
/opt/bitnami/ctlscript.sh restart apache
You should now be able to access your application in: http://localhost/myapp

I'm able to run the project with the development server. In Pycharm, I went to Tools -> Run manage.py Task -> run server
And then executed the url:
http://localhost:8000/myapp/
But still trying to figure out why it is not running with apache in Bitnami stack

Related

Django deployment not loading static files in Google app engine

Hi I am trying to deploy django app on Google app engine. My Django app works fine in the locally but in google app engine it is not working. I checked and found that issue is with my static files. My static files are not getting loaded in app engine.
********.appspot.com/static/youtube/about.css
Not Found
The requested resource was not found on this server.
Its been two days I have trying to follow answers on various forums but none worked for me. I have following in my settings.py
my settings.py code snapshot
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'youtube',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Just for your information, I tried changing STATIC_ROOT to 'static' but didn't work.
STATIC_ROOT = 'static'
STATIC_URL = '/static/'
My directory structure:
my project dir structure
My HTML file from app youtube, and app directory structure
about page html file snapshot
<link rel="stylesheet" type="text/css" href="{% static 'youtube/about.css' %}">
My App.yaml
runtime: python
env: flex
runtime_config:
python_version: 3.7
entrypoint: gunicorn -b :$PORT yt_analytics.wsgi
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
I have run below command to test on local server and then deploy on Google app engine.
python manage.py runserver
python manage.py collectstatic
gcloud app deploy
When I run collectstatic command I see it is creating new folder named static inside my project root directory.. i.e. yt_analytics/static
Now I see I have two static directories in my project--
yt_analytics/static
yt_analytics/youtube/static
When I ran findstatic command I got:
python manage.py findstatic youtube/about.css
Output:
Found 'youtube/about.css' here:
/home/work/Mirror/Projects/django/ytwebsite/yt_analytics/youtube/static/youtube/about.css
Have exhausted all forums and answers before asking this question. I am unable to deploy my app on GAE. Your help is appreciated.
Adding below code in urls.py after #Ivan Starostin's comment and it worked for me.
from django.conf.urls import url, include
from django.views.static import serve
urlpatterns = [
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Thanks Ivan for your valuable comment.

Why swagger documentation doesn't work on PythonAnywhere domain?

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

makemigrations not able to find app within INSTALLED_APPS

I am trying to port an existing django 1.4 project to django 1.7
Here is my tree structure before I ported the project to django 1.7
Project
- MainApp
- manage.py
- settings.py
- another_sub_app
- another_sub_app2
While porting the project I had to move manage.py and sub_apps one level up.
Project
- another_sub_app
- another_sub_app2
- manage.py
- MainApp
- settings.py
I used "South" for database migrations and had to use "python manage.py schemamigration " to make migrations. Now (after porting) I will be using "python manage.py makemigrations " to migrate app specific model changes.
However, while running "python manage.py makemigrations " I am getting:
App 'app_name' could not be found. Is it in INSTALLED_APPS?
I have the app under INSTALLED_APPS and due to the structural changes I made I also tried including . inside INSTALLED_APPS. But this shows the same error again.
My question is has anyone tried porting a project to django 1.7 and has had similar issue?
I haven't tried porting a project to django 1.7. However, I use django 1.7 and your settings.py should look like this
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'your_app_name', #In your case, MainApp
'your_app_name', #In your case, another_sub_app
'your_app_name', #In your case, another_sub_app2
)
And the tree structure is
Project
- Project
- settings.py
- MainApp
- another_sub_app
- another_sub_app2
- manage.py

More Django static 404 issues - paired with ember

I've seen lots of different posts regarding the Django static file issues. Unfortunately, none of them seem to help me. My django admin css is fine, however static files are giving my a 404 error. Here is a description of what my problem:
In settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = '/Users/me/develop/ember/myproj/static/'
In urls.py:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My project directory:
> static
> myproj
> app
> templates
> ember.hbs
> img
> test.jpg
Inside my ember.hbs I reference the test.jpg, but I get a 404 error. It's weird because I can actually pull up the image using:
file:///Users/me/develop/ember/proj/static/myproj/img/test.jpg
I have no idea what to do to fix this, I've tried using the STATICFILES_DIRS method mentioned here: https://docs.djangoproject.com/en/1.8/howto/static-files/
but nothing seems to be working.
Thanks in advance!
More information: Django 1.8 and Ember-CLI.
I'm hosting the ember project within the static dir.
I'm running python manage.py runserver and running django on port 8000 at the same time as running ember s and running the application on port 4200. I'm using CORS-headers (https://github.com/ottoyiu/django-cors-headers/) to allow cross-site calls on development.
Static files will be discovered in "apps" that you create (found in INSTALLED_APPS) or in additional directories that you specify in STATICFILES_DIRS.
Let's say I did:
django-admin.py startproject myproject
And then:
cd myproject
./manage.py startapp myapp
mkdir myapp/static
mkdir myproject/static
Would give you a directory structure that looks something like
/Users/me/develop/myproject/
> myapp/
> migrations/
> static/
> __init__.py
> admin.py
> models.py
> tests.py
> views.py
> myproject/
> static/
> __init__.py
> settings.py
> urls.py
> wsgi.py
> manage.py
And then in settings.py you add "myapp" to INSTALLED_APPS.
In this structure, myapp/static/ files will be automatically discovered by Django because it is an installed app with a static directory. However, myproject/static/ files will NOT be discovered automatically because "myproject" is not in INSTALLED_APPS (and it shouldn't be).
This is because of the default STATICFILES_FINDERS setting, which is:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
]
The first finder, AppDirectoriesFinder, scans the directories of all of your INSTALLED_APPS, and discovers any "static" folders they might contain.
The second finder, FileSystemFinder, scans extra directories that you specify in STATICFILES_DIRS, which is an empty list by default.
So if you want those files in /Users/me/develop/myproject/myproject/static/ to be discovered (as you probably do), you can add this to your settings.py:
from os import pardir
from os.path import abspath, dirname, join
# Get the root directory of our project
BASE_DIR = abspath(join(dirname(__file__), pardir))
# Add our project static folder for discovery
STATICFILES_DIRS = (
# /Users/me/develop/myproject/myproject/static/
join(BASE_DIR, 'myproject', 'static'),
)
Now, all of this is separate from STATIC_ROOT, which is a place where all static files are copied when you run ./manage.py collectstatic. For projects just starting out, this is usually in the project root folder: /Users/develop/me/myproject/static/
So we would add, in settings.py:
# /Users/me/develop/myproject/static/
STATIC_ROOT = join(BASE_DIR, 'static')
But this is only really used in production—it's a way to compile all of your static assets into a single spot, and then point /static/ at it with your server (e.g. NGINX).
During development, you can skip collectstatic altogether, and have Django discover and serve your static assets on the fly. Django provides a "serve" view to do this in django.contrib.staticfiles.views.serve.
Try this in your urls.py:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.staticfiles.views import serve
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', serve),
]
In your example, you are asking Django to serve files from STATIC_ROOT, which is also fine, but requires you to run ./manage.py collectstatic first, and every time your static assets change—which can be a hassle.
Ember and Django really shouldn't live in the same project. They are completely different frameworks with different tooling.
Think of your Django project as the backend application that serves an API. Think of your Ember project as one of possibly many clients that will consume your API. Others might include mobile applications, partner services, and so on.
When I start a new project that is going to use Django and Ember, I start a Django project called "myproject":
django-admin.py startproject myproject
And separately, an Ember project called "myproject-web":
ember new myproject-web
In development, I'll have two tabs open in Terminal.
First tab:
cd myproject
./manage.py runserver
Second tab:
cd myproject-web
ember serve
In production, I'll have two different servers, one at app.myproject.com that serves the Django application, and another one at myproject.com that serves the Ember application.
Later on, I might start a Swift project in a repository called "myproject-ios", and a Java project called "myproject-android". You get the idea.
In order to serve the static files into your django project you just need to add these lines to your `settings.py
STATIC_URL = '/static/'
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
and then in the main stucture of your project dictory needs to be like
>Project_dir
>App_dir
>static_dir
>templates
>manage.py
place your css, js, and other static contents into your static_dir

Django 1.4.1 Admin CSS not showing

I know this question has been asked many times but none of the solutions seems to work for me. I just started using django and I'm having issues trying to get the CSS for the admin panel to show when I use runserver.
When I'm in: localhost:8000/admin/ none of the CSS shows up
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/dashboard.css" />
That is what is shown in the in the HTML of the admin page, which those URL's it links to ends up becoming http://localhost:8000/static/admin/css/base.css which doesn't link to anything.
In settings.py I have
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'codeapp',
'django.contrib.admin',
'django.contrib.admindocs',
)
STATIC_ROOT = '/Users/datboitom/Sites/Codebase/codebase/codeapp/static/'
STATIC_URL = '/static/'
I'm just not too sure what else to do. If can help me to fix this issue that would be great. I've ran python manage.py collectstatic and the static files are in /Users/datboitom/Sites/Codebase/codebase/codeapp/static/ folder. There just doesn't seem to be any link between these files and whats trying to be loaded. One is trying to load off the localhost url and not the path of where its located on the computer.
Just in case someone run into this error again. While in Debug=False mode, Django won't serve static files for you anymore. Your production server should take care of that. But, if you still want to test your app locally in debug=false you should run the devserver in insecure mode:
manage.py runserver --insecure
Taken from here: Why does DEBUG=False setting make my django Static Files Access fail?
What is your setting like your server?
According to the documentation, you should not write anything at STATIC_ROOT
Leave STATIC_ROOT blank and instead, then, put the path in STATICFILES_DIRS.
Hope this helps
I guess you are missing the following in your settings.py file
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Config ERROR
in setting.py, you should add 'django.contrib.staticfiles' in INSTALLED_APPS
the codes show:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
......
OK,try it.
I had this problem just recently after changing dev machines.
It turned out that my new machine wasn't set to run django apps in debug mode locally, so adding
DEBUG = True
To my dev machine's settings.py sorted it.

Categories

Resources