How to serve django and gatsby on single vhost? - python

I'm trying to deploy gatsby based frontend with django based backend under a single domain. It will rely on Apache and mod_wsgi. In a perfect world it should work as following:
https://my-domain.com/ - gatsby frontend
https://my-domain.com/admin - django
https://my-domain.com/api - django
I can see two possibilities:
Django is aware of frontend. Serve everything via Django, setup / as a STATIC_URL.
Django is not aware of frontend. Serve /api and /admin via django. / is handled by a webserver.
I feel more comfortable with the second approach, however I do not know how to configure VirtualHost for such a scenario. The firstch approach looks like an ugly hack.
How should I proceed with that?

After compiling your gatsby project, it should be served by django as a static page.
First: The gatsby dist should be in your static_private path.
Second: In your django project, you will define a URL for / that will call an index view let's say.
Finally: in your view you should render index.html of your gatsby dist.
urls.py:
from django.contrib import admin
from django.urls import path, re_path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/', include('apps.urls')),
path('/', views.index),
]
views.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Note that if you are handling routing in your frontend your url pattern for the index view should be like this : re_path('^.*$', views.index)
If you are hosting your django app on heroku you will need the whitenoise middleware and set it up in your settings.py :
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
A doc is available here: https://devcenter.heroku.com/articles/django-assets#whitenoise

Related

Routing error using Wagtail v2 with Django v1.11

Can't seem to find anything on this so far, I'm guessing I have something misconfigured somewhere.
I have a pre-existing Django app and am trying to use Wagtail to add a blog in. I have installed as per the instructions and I can access the default landing page so it seems to be installed however I'm stuck with the below error when attempting to access the admin for it and am not sure how to proceed. Guessing its something to do with not defining namespaces somewhere, the Wagtail docs say that its compatible with Django v1.11 but their integration documentation is for Django v2 specifically and is using re_path etc.
'wagtailadmin_api_v1' is not a registered namespace
My main project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include('blog.urls', namespace='blog')),
url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')),
url(r'^autocomplete/', include(ac_urls, namespace='ac')),
url(r'^', include('website.urls', namespace='website')),
]
My blog app's urls.py (which is fresh from a "python manage.py startapp blog")
from django.conf.urls import url, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
app_name = 'blog'
urlpatterns = [
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
]
When I attempt to access localhost:8000/blog/ I get the default wagtail landing page, if I go to localhost:8000/blog/cms/ I get the above error though. If I go to localhost:8000/blog/documents/ I get a 404 as well.
Not sure where I'm going wrong, have tried using a version of wagtail that still used the Django v1.11 way of routing in urls.py but I got the same error!

unable to connect django and angular 2

I want to connect my angular 2 app to django but how can i connect because both have different servers. I also read about cors but didn't work. Please suggest me some simple way to connect both of them
views.py
# Home Page
def index(request):
return render(request, "index.html")
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
]
.angular-cli.json
"root": "src",
"outDir": "../userRecord/templates",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
userRecord is Django Server
Thanks in Advance
Hello I found a GitHub Repo without any Error
Here I posting the URL of that repo
https://github.com/badcoder28/djangular2
There's no problem serving the django backend and the angular frontend from different domains, but you do have to set up CORS correctly for it to work.
To configure that, you'll have to setup Django to return the CORS headers and allow the domain where your frontend lives. These might be helpful:
https://github.com/ottoyiu/django-cors-headers
http://www.django-rest-framework.org/topics/ajax-csrf-cors/
You need to serve angular app using django server.
In order to do this you need to keep angular bundled file in django static directory.
If you are using angular cli then you need to change outDir: "../../static/app" property in .angular-cli.json file to your django static directory, so whenever you build your app, your compiled file will be in django static directory.
Refer these static files into your django html page which django serve whenever you launch your app.
Happy Coding
EDIT
In urls.py you can configure like this
urlpatterns = [
url(r'^', views.index, name='index')
]
In your views.py have the index action like this.
def index(request):
return render(request, "your template path")
Make sure your def index action should return index.html.
I hope it will help you.

Django backend not returning JSON on production server, getting 404

I have a Django backend that returns json data. I'm able to get data back on my localhost but got a 404 on production server. I'm running nginx in front of gunicorn server. Any ideas why I'm getting a 404? Shouldn't this be able to work to retrieve json data, or do I need to use django rest framework and implement viewsets to make this work?
Not Found
The requested URL /about was not found on this server.
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about', about.get_info),
]
about.py
from django.http import JsonResponse
def get_info(req):
return JsonResponse({"test": "hello"})
The problem is inside url.py. The way the rules are defined currently, it would only allow you to open about/ and admin/, i.e. with the / at the end. To fix this, you can define the URLs as following:
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^about/$', about.get_info),
]
Now you should be able to use both admin/ and admin to access the page.

Serving static index.html in django

Thing is, I have an angular + cordova app on one module. I just want the REST api from django for the webserver. no views or anything (well only to serve the angular views).
How can I serve the static index.html using django? do I need different project structure? I'm a newbie to django.
Thanks in advance!
You can use TemplateView directly:
from django.views.generic import TemplateView
...
url(r'^$', TemplateView.as_view(template_name='index.html'))
Remember you need to configure your templates folder to call .html files by name only.
First, place this index.html page in your server/templates folder so Django knows about it.
Then, do this in your URLs.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html')),
]

Django not reflecting changes to urls.py

I am setting up a login system for a Django site, which is running on an Nginx server. I'm getting the following debug 404 page:
Using the URLconf defined in it.urls, Django tried these URL patterns, in this order:
^admin/
^login/ [name='main_login']
^$ [name='index']
^laptops/
^assets/
^mardes/
^users/
^jobs/
^static\/(?P<path>.*)$
The current URL, account/login/, didn't match any of these.
This appears to be using an old version of the it.urls file; the current one looks like this:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls) ),
url(r'^account/', include('account.urls', namespace='account'), name='account' ),
url(r'^laptops/', include('laptops.urls', namespace='laptops') ),
url(r'^assets/', include('assets.urls', namespace='assets') ),
url(r'^mardes/', include('mardes.urls', namespace='mardes') ),
url(r'^users/', include('users.urls', namespace='users') ),
url(r'^jobs/', include('jobs.urls', namespace='jobs') ),
url(r'^', TemplateView.as_view(template_name='it/index.html'), name='index' ),
) + staticfiles_urlpatterns()
Which, as you can see, has no r'^login/' pattern.
I have set LOGIN_URL to '/account/login/' in my settings.py file, however the #login_required decorator is loading '/login/'. The new url is also ignored when I specify login_url='/account/login' in the #login_required call.
I have restarted Nginx (both by starting and stopping, and running 'restart'), this has made no different. I have also set 'sendfile=off' in the nginx.conf file. I am guessing there is cache stored somewhere (re: How to clear the cache of nginx?).
Please let me know if you need more details.
Nginx doesn't run Django alone, but it should interact with a wsgi app server like uwsgi or gunicorn.
Maybe you should restart the wsgi app server instead of nginx.
good catch #bcvery1. Also for those who are running django apps via passenger, restarting nginx or re-creating restart.txt file should also work to see those changes.

Categories

Resources