Django: dynamically sharing static files - python

I am working on a project in django 1.4.4, what i am trying to achieve is i have website's index pages mapped with a unique url in database. whenever url hits that url, it should load that website.
I am able to load the index.html(easy) using http://lclhst.com/landing/one-bush
But the problem is the static files that web page auto loads.
so what i have right now is (website) in media/uploads/{unique-no}/index.html and other media files. I tried to make an all url accepting view but i think django auto-tries to serve media files when requested so all the hits like http://lclhst.com/landing/one-bush/_include/images/dot.jpg are being 404'd.
i am looking for any mistake in my approach or some better approach.

Related

How should I manage my images for my django app

Here I am once again asking a question that might seem stupid:
I am new to web development. So far I've made some practice projects with Django and I've deployed two projects so far as github pages but they were simple static websites (no django).
Now I'm making a web application for my mom's business with Django.
I don't know what I should do with all the images I need for the website.
In the past I have uploaded all jpg and png images to imgur and copied the link in my static websites html
But now I have more images and I am going to pay for a server for them application. I have two types of images:
Images that will always appear on the page. Because that's part of the design
Images that the admin can upload when creating a new post for the page
So my question is:
Should I have all images in the statics folder for my application?
And call them like this:
<img src="{% static 'website/images/home-slider/myimage.css' %}">
And for the other images should I save a similar path in the database?
I just have no idea if things should be done different for production
Images used as a part of html files are served within static files and images uploaded by the user as a part of creating new post are saved as media files. You usually have seperate static and media root for saving files to avoid confusion.
Media files are served as '/media' as static are served as '/static'.

Can I Use One view In a Django Project?

I´m new to web dev,
and I was wondering if it´s possible to make a website, that just need to present information of a company (HTML), in just one view.
Like rendering the entire bootstrap in one view.
Yes, you can serve your HTML code through a TemplateView.
So if your entire single page application sits in home.html you could definitely do this. But there is no point in using Django for only that purpose. You would rather want to serve your static HTML page from a classic web server like nginx or apache.
I don't know why would you want to do that.
You can use different html files which will be served as your website templates. You can also extend the files using a simple base.html file. This will help you if you want to open other links when people click on different links on the website.
See this example: https://github.com/singh1114/Djangosite/tree/master/duggal/webportal/templates/webportal.
For using this you have to know more about views and urls.
For making scrollable things, you need to know the concept of ids in HTML.
For example
http://yoursite.com/#your_name will try to find id your_name in the HTML. This way you can create scrollable things.

How can I added some sub html project to a django project

Now I have a django project and some small html project (in fact, some html5 games). How can I added the html site to the django project?
For the each html project has ref a lot of css, img and javascript in its own folder, and different html site are in different folder.
Hence, I can not now just do like this:
t = get_template('htmlprojectfolder/index.html')
html = t.render(Context())
return HttpResponse(html)
I think my problem is:
"how can I jump to a html index page with its static files correct linked?" (Clealy, I can not read just the index page as a template file.)
Thank you for help!
I think the best way to add static, non-Django files that shall be accessible via a Django project would be to add the files to the STATICFILES_DIR. You can read more about static files here in the Django documentation.
Another, and possibly better, way to handle this would be to let the webserver serve the static files separately from Django.

Serve Static Pages from S3 using Django

I'm planning to build a Django app to generate and later server static pages (probably stored on S3). When users visit a url like mysite.com/static-pages/12345 the static file in my S3 bucket named 12345.html should be served. That static file might be the static html page of a blog page my site has generated for the user, for example.
This is different from including static resources like CSS/Javascript files on a page that is rendered as a Django template since I already know how to use Django templates and SQL databases - what's unfamiliar to me is that my "data" is now a file on S3 rather than an entry in a database AND that I don't actually need to use a template.
How exactly can I retrieve the requested data (i.e. a static page) and return it to the user? I'd like to minimize performance penalties within reason, although of course it would be fastest if users directly requested their static pages from S3 (I don't want them to do this)".
A few additional questions:
I've read elsewhere about a django flatpages app which stores html pages in a database, but it seems like static html pages are best stored on a filesystem like S3, no?
Is there a way to have the request come in to my Django application and have S3 serve the file directly while making it appear to have come from my application (i.e. the browser url still says mysite.com/static-pages/12345, but the page did not go through my Django server)?
Thanks very much!
Amazon S3 doesn't support URL rewriting (it's not a webserver), so you're going to have no choice but to proxy the requests to a web server or service that can rewrite the urls for you.
You could use a web server you control and follow the instructions here to have apache rewrite the URLs, but that seems somewhat wasteful when the whole point is loading a static website.
Alternatively, I have a solution that might work if you want to stay purely in S3:
You have the option to specify an HTML document that will be returned to the user's browser in the case of 404 - the error document. You could create a tiny HTML page that checked to the current URL, and simply changed window.location to go to "rewritten" url without the .html extension:
<html>
<script>
var slash = window.location.lastIndexOf("/");
var dot = window.location.lastIndexOf(".");
if (slash < dot) && (dot != -1) {
window.location = window.location + ".html";
}
</script>
</html>
Obviously you'd want to make it more robust, but you get the idea.
The downside is that each request for your static pages using the url will make an extra round-trip from the user's browser to your server (once for the 404 page, and then once to get the real page).
Also, you'd need to adjust my above code to avoid triggering a 404 loop in the case of an actual url being incorrect by adding a check something like:
var loopcheck = window.location.IndexOf(".html");
if (lookcheck != -1) {
window.location = "real404.html";
return;
}
Hope that helps.
You can just create index.html inside /static-pages/12345/ folder and it will be served.

How to convert Django dynamic page to static HTML file?

The current website is running on Django, Gunicorn and nginx. I want a way to convert the current front page into a static HTML page and want nginx to serve this static page instead of going through the whole web stack. I want the front page to load faster. This can be done manually, but is there a tool integrated with Django or Gunicorn that automatically convert certain page into static and serve those pages?
put it in your /media/ folder
then just point to
some.url/media/html/some_static_html.html

Categories