My setup consists of a Django web application + Django admin served by Tornado.
Serving Django works fine except for Django admin. When I open http://localhost:8000/admin I only see HTML without any CSS.
Fetching admin assets results in a 404.
My configured routes:
application = WSGIHandler()
container = WSGIContainer(application)
tornado_app = tornado.web.Application(
EchoSockjsRouter('/websocket').urls+
[
(r"/public/(.*)", tornado.web.StaticFileHandler, {"path": "static/"}),
('.*', tornado.web.FallbackHandler, dict(fallback=container)),
])
My assumption was, that all assets for Django admin are also served by the FallbackHandler.
If not, what do I need to do to also have admin assets served properly?
Related
I´m currently working on an application: Django backend and angular frontend. Everything works fine, but after refreshing one page in the browser I don´t get the angular response anymore. But what I get is a successful answer from my Django server (HTTPResponse: 200).
My Django urls.py is as followed:
from django.urls import path
from quickstart import views
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('images/', views.ImageList.as_view()),
path('images/<str:id>/', views.Image.as_view()),
path('images/<str:id>/explanations', views.Explanation.as_view())
]
My app-routing.modules.ts:
const routes: Routes = [
{path: 'home', component: MainComponent},
{path: 'images', component: ImageComponent, children: [
{
path:':id', component: ImageDetailComponent
}
]},
{path: '', redirectTo: '/home', pathMatch: 'full'},
];
#NgModule({
imports: [RouterModule.forRoot(
routes
)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Angular production build is served as a static file. it makes the api request to the backend server for the CRUD operations.
what you need to do is serve the static files in angular dist(most probably the index.html file in the dist folder) folder.
using some sort of routing in django (suggestion have a prefix for the API or web app for django to know what are you asking for. look at this)
or have a standalone service hosted some where to serve Angular site(have a look at this) and let angular handle the routing.
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.
I've deployed my Django (DRF API) project on DigitalOcean apps. The path "/" is used by my static site that uses this API so I set route for this component to "/api" which works correctly.
The problem:
I go to /api/admin/ and it redirects me to /admin/login but the django is served on /api url so this URL is invalid.
Do you know how to make this work?
Is there a way to tell django to use absolute URL everywhere?
urls.py
urlpatterns = [
...
path("api/admin/", admin.site.urls),
...
]
My django app's urls are setup as follows for my development machine:
urls.py:
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^signin$', signin, name='signin'),
url(r'^signout$', signout, name='signout'),
url(r'^advisor/', include('apps.advisor.urls', namespace='advisor')),
]
This setup works fine while developing the app using Django's builtin server (e.g. http://127.0.0.1:8000). I have to deploy the app to a machine and the Django app is exposed by the following url:
http://ip address/appname/
I'd like to have my urls.py file configured so the regular expression in the url tests to see if the url request starts with "appname" or "" (empty string). This method would mean I don't have to create to urls.py files (dev and prod versions). Thanks for any help!
You do not have to do this at all. When your app is deployed via WSGI, the server should pass on the SCRIPT_NAME parameter which identifies the path it is mounted at. As long as you have consistently used {% url %} and reverse() throughout your app rather than hard-coding links, everything will just work.
I have a single Django app that handles two (or more) parts of site, for example an "admin" and an "api" sections of the site. I also have normal html pages for the rest of the site, where no Django is needed.
I want to have the static html site on the root of my site, and the Django app on some sub-locations, for example
www.example.com -> Apache static html site
www.example.com/admin/ -> Django app, serving the pages for the admin
www.example.com/api/ -> Django app, serving the pages for the api
At first I try to do so all form Django, using the urls like so:
# my_app/urls.py
...
url(r'^admin/', include('admin.urls')),
url(r'^api/', include('api.urls')),
url(r'^(?P<path>.*)$', ??????????),
...
But I couldn't figure out a good way to delegate the serving of those static pages to Apache. (It did work if I use url(r'^(?P<path>.*)$, 'django.views.static.serve', {'document_root': '/path/to/static/site'}), but the Django documentation forbids to use that on a production server.)
Then I tried with the apache configuration:
# httpd.conf
...
DocumentRoot /path/to/static/site
WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api /path/to/django/my_app/wsgi.py
...
This worked as far as requests to root returned the static site and requests to both "admin" and "api" would return the Django site, but within Django I found no way to distinguish if the request came from '/admin' or from '/api'. (Plus I'm not sure if having two WSGIScriptAlias pointing at the same wsgi might cause some problems.)
If someone knows of a way to achieve this without having to split my Django app into two (or more) parts it would be greatly appreciated.
I had the almost exact same problem. In Apaches virtual host config for the site use
WSGIScriptAliasMatch ^(/(admin|api)) /path/to/django/my_app/wsgi.py$1
instead of
WSGIScriptAlias /admin /path/to/django/my_app/wsgi.py
WSGIScriptAlias /api /path/to/django/my_app/wsgi.py
Check these Q&A for further information:
Django (wsgi) and Wordpress coexisting in Apache virtualhost
Reconfiguring Apache to serve website root from new php source and specific sub-urls from old django site