Serving static index.html in django - python

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')),
]

Related

How to serve django and gatsby on single vhost?

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

Getting error when I am trying to fetch my main url patterns

I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error
Page not found (404)
Request Method: GET
URL: http://127.0.0.1:8000/
the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/
blog/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to
False, and Django will display a standard 404 page.
when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/
here is my main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app
And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.
If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.
Create a view that will be your front page.
From there you should link to the other areas of your website.
Don't direct that to admin, that's ridiculous.
You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.
To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'),
name='home'),
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.
We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:
# app1/urls.py
from django.urls import path
from .views import AppContentView
urlpatterns = [
path('content/', AppContentView.as_view(),
name='app_content'),
]
This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.
If you think of urls configurations like a tree it might help too:
Project Level Url Configs.
|
|
|
___________________________
| |
| |
App 1 Url Configs. App 2 Url Configs.

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.

Is it possible to serve a static html page at the root of a django project?

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.

Django - render HTML template straight from urls.py

I have a Django app (I'm fairly new so I'm doing my best to learn the ins and outs), where I would like to have a url endpoint simply redirect to a static html file in another folder (app).
My project file hierarchy looks like:
docs/
- html/
- index.html
myapp/
- urls.py
My urls.py looks like:
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^docs/$', RedirectView.as_view(url='/docs/html/index.html')),
)
However, when I navigate to http://localhost:8000/docs I see the browser redirect to http://localhost:8000/docs/html/index.html, but the page is not accessible.
Is there any reason that the /docs/html/index.html would not be avalable to the myApp application in a redirect like this?
An pointers would be greatly appreciated.
NOTE: direct_to_template has been deprecated since Django 1.5. Use
TemplateView.as_view instead.
I think what you want is a Template View, not a RedirectView. You could do it with something like:
urls.py
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^docs/$', direct_to_template, {
'template': 'index.html'
}),
)
Just ensure the path to index.html is in the TEMPLATE_DIRS setting, or just place it in the templates folder of your app (This answer might help).
I'm pretty sure Django is looking for a URL route that matches /docs/html/index.html, it does not know to serve a static file, when it can't find the route it is showing an error

Categories

Resources