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.
Related
I develop an application using Django and React. I want to serving Django and React together. I create an build in react. I use it in Django. This is urls.py ;
urlpatterns = [
url(r'^$', views.index),
]
and views.py;
def index(request):
return render(request, "build/index.html")
When I run development server there is no any error but when ı want to go url directly ;
http://www.x.com:8000/accounts
I got page not found (404) error.
Well, do you have an urlpattern for /accounts? If your example is complete then you get 404 because Django doesn't know what to return for /accounts. The only URL your Django server supports, as per your example code, is index available at http://www.x.com:8000 (maybe even http://www.x.com:8000/ (notice the trailing /) depending on your APPEND_SLASH settings).
You need to to add an urlapptern and view (and probably a template) for /accounts. Something like (not tested)
urlpatterns = [
url(r'^$', views.index),
url(r'^accounts$', views.index),
]
def accounts(request):
return render(request, "build/accounts.html")
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
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.
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')),
]
I have a django app hosted on pyhtonanywhere. The app resides at username.pythonanywhere.com/MyApp. I would like to serve a static html page at username.pythonanywhere.com. Is this possible? Essentially it would serve as an index linking to /MyApp, /MyApp2, and other future apps.
I can't seem to find any info on how to do this, but I assume I have to modify mysite/mysite/urls.py as navigating to root currently gives me a 404 with messages about failing to find a match in urls.
urlpatterns = [
url(r'^/$', ???),
url(r'^admin/', include(admin.site.urls)),
url(r'^MyApp/', indluce('MyApp.urls')).
]
The previous is my best guess (a bad one i know). That should (correct me if i'm wrong) match the root URL, but I have no idea how to say "hey django just look for a static file", or where that static html should live, or how to tell django where it lives.
Try not to cut my head off. I'm brand new to django.
P.S. I'm using django 1.8 in a virtualenv on PA
Of course you can. In cases where I just need to render a template, I use a TemplateView. Example:
url(r'^$', TemplateView.as_view(template_name='your_template.html'))
I usually order my URL patterns from most specific to least specific to avoid unexpected matches:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^MyApp/', include('MyApp.urls')),
url(r'^$', TemplateView.as_view(template_name='your_template.html')),
]
As far as where Django looks for templates, it's up to your configuration to tell Django where to look: https://docs.djangoproject.com/en/1.8/topics/templates/#configuration
On PythonAnywhere, you can use the static files facility of your web app to serve the static file before it gets to Django:
Put a file called index.html in a directory and then point a static file entry to that directory. If the static file URL is / and the directory is the one with the html file in it, the file will be served at /.
Be aware that you don't want the directory to be above any of your code or you'll expose your code as static files i.e you don't want to use the directory /somewhere/blah if your code is in /somewhere/blah/code, you'll want to put it in /somewhere/no_code_here
I have been tinkering with Django version 3.2, and I was able to serve a static HTML page at the root of the project after creating a Django app as in their official tutorial like the following in the root urls.py file:
from django.views.generic import TemplateView
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home/base.html')),
]
This is given that home/base.html is placed where the default template folder is. If I simply re-use the polls templates directory I can create a folder like polls/templates/home/ to store base.html, for example.