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.
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.
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),
...
]
I am trying to refer to the WordPress blog through my sub domain. example -
blog.example.com
WordPress is hosted on GoDaddy
My main app (main domain) is on Django (hosted on Heroku). Have site maps set up on Django apps as well under:
example.com/site maps.xml
How do I integrate the two sitemaps so that when I go to example.com/site maps.xml I should see sitemaps from both? Is there a way?
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.
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?