I have the following codes for deploying a Django 1.9 project named deploy_project on a CentOS 7 server.
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deploy_project.settings")
application = get_wsgi_application()
The above was generated by default.
httpd.conf
WSGIScriptAlias / /var/www/deploy_project/deploy_project/wsgi.py
WSGIPythonPath /var/www/deploy_project
<Directory /var/www/deploy_project/deploy_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
My project directory is in /var/www/deploy_project. Inside that I have an app called Deploy, the project settings folder called the same as the project name deploy_project and the manage.py file. I also have a db.sqlite3 file as I am not using MySQL, but my app just runs a view which shows Hello World. I am not using the database.
When I visit the server IP from browser, I receive a 404 Not Found page with the message The requested URL / was not found on this server..
tree output of project folder
├── db.sqlite3
├── deploy
│ ├── admin.py
│ ├── admin.pyc
│ ├── apps.py
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── templates
│ │ ├── 404.html
│ │ └── deploy
│ │ └── hello.html
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── deploy_project
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── manage.py
project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('deploy.urls')),
]
app urls.py
urlpatterns = [
url(r'^$', views.Hello, name='hello'),
]
views.py
def Hello(request):
return render(request, "deploy/hello.html", {})
hello.html
Hello World!
Being the dumb developer that I am, I didn't restart apache service, so my settings didn't take effect.
For CentOS 7 my command was:
sudo systemctl restart httpd
That solved my problem. Thanks for your time #BurhanKhalid.
Related
I have a Django project with 2 apps and was writing some functions in a utils.py file in one of the apps. I wanted to break this up into two separate files in their own subdirectory so I created a new directory 'utils' a level below the app directory and placed the two utils1.py and utils2.py files in there.
I had some issues with importing something from the other app so I ended up scrapping this idea and moving everything back into one file in the base directory of the original app, exactly like it was before. Now when I runserver it is not picking up any new files that are created within apps. Not just the ones that I recreated but any new files. Files that were created prior to the change are running just fine.
So, in summary new utils.py files that I recreated in the app directory are not running when the dev server is started, and when I try to run one of them manually they run like any other python file, but imports from other locations in the project are not being recognized.
No other changes were made and new files were running perfectly fine before the directory changes.
After the changes:
├── app1
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── permissions.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── manage.py
├── project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── app2
├── __init__.py
├── admin.py
├── apps.py
├── utilities <--- added
├── util1.py
└── util2.py
├── migrations
├── models.py
├── serializers.py
├── tests.py
├── urls.py
└── views.py
After reverting back to previous structure (not working):
├── app1 <--- new files created here aren't running
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── permissions.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ ├── utils.py
│ └── views.py
├── manage.py
├── project
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── app2 <--- new files created here aren't running
├── __init__.py
├── admin.py
├── apps.py
├── util1.py <--- moved back into app directory
├── migrations
├── models.py
├── util2.py <--- moved back into app directory
├── serializers.py
├── tests.py
├── urls.py
└── views.py
I've tried clearing the pycache files, restarting the dev server, restarting terminal, etc. to no avail.
I figured out what was going on. My assumption was that any new python files in an installed app would be automatically run, but something from the file needs to be imported first from somewhere else in the project. Before the changes I had an import in the utils.py file so the dev server was running it, but after the changes there were no imports from elsewhere in the project. Issue is fixed and working now.
I have a Django 2.0 project using celery 4.2.1 and redis 2.10.6. The django project has two apps, memorabilia and face_recognition. I have it all successfully running tasks with django running on my development machine. I uploaded everything to my git server, then installed the apps on my laptop from git, updated all requirements, etc. Both are Ubuntu machines. I am not using django-celery.
When I try to run celery -A MemorabiliaJSON worker -l debug,
I get an exception saying ModuleNotFoundError: No module named 'face_recognition.tasks'
I am not sure how to fix this, as the same code base is running on my development machine.
My file structure is:
├── celery.sh
├── face_recognition
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── tasks.py
│ ├── tests.py
│ └── views.py
├── __init__.py
├── manage.py
├── memorabilia
│ ├── admin.py
│ ├── apps.py
│ ├── fields.py
│ ├── fixtures
│ ├── __init__.py
│ ├── logs
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── storage.py
│ ├── tasks.py
│ ├── templates
│ ├── tests
│ ├── urls.py
│ ├── validators.py
│ ├── views.py
│ ├── widgets.py
├── MemorabiliaJSON
│ ├── celery.py
│ ├── default_images
│ ├── documents
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings
│ ├── static
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── __pycache__
│ ├── celery.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── requirements.txt
└── tests
MemorabiliaJSON/celery.py
# http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.apps import apps
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MemorabiliaJSON.settings.tsunami')
app = Celery('MemorabiliaJSON')
app.config_from_object('django.conf:settings', namespace='CELERY')
#app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
(memorabilia-JSON) mark#octopus:~/python-projects/memorabilia-JSON
face_recognition/__init__.py
default_app_config = 'face_recognition.apps.FaceRecognitionConfig'
memorabilia/__init__.py
default_app_config = 'memorabilia.apps.MemorabiliaConfig'
INSTALLED_APPS has these two apps
'memorabilia.apps.MemorabiliaConfig',
'face_recognition.apps.FaceRecognitionConfig',
I've been trying to deploy an app to pythonanywhere but the page is just blank, because main.6f259c1b.js file throws error`
Uncaught SyntaxError: Unexpected token <
`
I've been following the instuctions on this article https://www.fusionbox.com/blog/detail/create-react-app-and-django/624/ and this https://www.techiediaries.com/create-react-app-django/
both articles suggest to create a view with following content
class FrontendAppView(View):
"""
Serves the compiled frontend entry point (only works if you have run `yarn
run build`).
"""
def get(self, request):
try:
with open(os.path.join(settings.REACT_APP_DIR, 'build', 'index.html')) as f:
return HttpResponse(f.read())
except FileNotFoundError:
logging.exception('Production build of app not found')
return HttpResponse(
"""
This URL is only used when you have built the production
version of the app. Visit http://localhost:3000/ instead, or
run `yarn run build` to test the production version.
""",
status=501,
)
and in app website/urls.py
urlpatterns = [
url(r'^', FrontendAppView.as_view())
]
Those instructions don't work for me. It's something that related to pushState routing, react-routing, I don't know. My app works ok in development server in localhost:3000, it only seen in pythonanywhere and local apache server with mod_wsgi.
This is my config of local apache(from Django documentation):
WSGIScriptAlias / /home/user/projects/myproject/myproject/wsgi.py
WSGIPythonHome /home/user/.virtualenvs/myproject
WSGIPythonPath home/user/projects/myproject/
<Directory /home/user/projects/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
This is root
DocumentRoot "/srv/http"
Part of my settings
REACT_APP_DIR = os.path.join(BASE_DIR, 'frontend')
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [
os.path.join(REACT_APP_DIR, 'build', 'static')
]
All software are latest release.
Maybe this comment solves my problem, I just don't understand it. https://github.com/facebookincubator/create-react-app/issues/1812#issuecomment-286511320
This is my localhost screenshot
My project directory structure
├── api_v0
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── myproject
│ ├── __init__.py
│ ├── local.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── frontend
│ ├── build
│ ├── node_modules
│ ├── package.json
│ ├── package-lock.json
│ ├── public
│ ├── README.md
│ └── src
├── manage.py
├── requirements.txt
├── static
│ ├── admin
│ ├── css
│ ├── js
│ ├── media
│ └── rest_framework
└── website
├── admin.py
├── apps.py
├── __init__.py
├── management
├── middleware.py
├── migrations
├── models.py
├── __pycache__
├── tests.py
├── urls.py
└── views.py
Review the styles referenced on the page and make sure that all the CSS files are referenced on the page using a <link> tag and not a <script> tag.
reference
also there are nginx adubting , you can do it for request your site adminstator help
I try to structure my project by putting applications under an "apps" folder, like so:
├── manage.py
├── mysite
│ ├── apps
│ │ ├── __init__.py
│ │ ├── myapp1
│ │ │ ├── __init__.py
│ │ │ ├── models.py
│ │ │ ├── urls.py
│ │ │ └── views.py
│ │ └── myapp2
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
And in mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
url(r'^mysite/apps/myapp2/', include('myapp2.urls')),
url(r'^admin/', include(admin.site.urls)),
)
There is something wrong with:
url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
url(r'^mysite/apps/myapp2/', include('myapp2.urls')),
I could not wire either myapp1 or myapp2 correctly, Django gives me "ImportError...no module named myapp1..." Any help?
You're missing a level in the relative path:
url(r'^mysite/apps/myapp2/', include('apps.myapp2.urls')),
myapp1 looks like it should work to me.
A note, comparing how you're trying to include myapp1 vs myapp2, it looks like you may have misunderstood the structure slightly. The URL has nothing to do with the code layout. This is completely valid:
url(r'^zimzam/allthethings/', include('apps.myapp2.urls')),
maybe like this:
include('mysite.apps.myapp1.urls')),
update
you can try:
add a file __init__.py in the mysite dir
I am relatively new to Django development.I have a css file inside a /static/css directory.
When I try to run the url no CSS is applied to my template. the python manage.py runserver window shows following error
[01/Jan/2013 20:00:40] "GET /home/prat/PROJECT_ROOT/SOURCE_ROOT/static/css/Style.css HTTP/1.1" 404 2207
Can someone please point me how to debug this. I have read multiple stackoverflow questions and added the following setting in my settings.py.
PROJECT_R = os.path.abspath(os.path.dirname(__name__))
PROJEECT_R = PROJECT_R + "../"
STATIC_ROOT = os.path.join(PROJECT_R, "static")
STATIC_URL = 'static/'
.
├── manage.py
├── README
├── SOURCE_ROOT
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── static
│ ├── css
│ │ ├── README
│ │ └── Style.css
│ ├── images
│ │ └── README
│ └── js
│ └── README
├── template
│ ├── base.html
Here's how I usually go about managing dynamic project root:
from os.path import dirname, realpath, join
PROJECT_ROOT = dirname(realpath(__file__))
And then further below, the static root:
STATIC_ROOT = join(PROJECT_ROOT, 'static/')
And then you reference static files like so:
{{ STATIC_URL }}css/Style.css
EDIT:
See the documentation for more information.