Django working with external HTMLs - python

I am writing a small Django local project (with no access to internet). Let's say i have offline standalone version of Wikipedia(I believe it's HTML5 format) as that: .
I am trying to write a simple Django front page where user would be able to click a button and be redirected to "wikipedia_for_schools/index.html" from where all the url control will be done by that offline wikipedia stand alone page.
How is it possible to make? :/ I have tried creating a link in django template as
Click to see wikipedia
but that doesn't work cause Django complains that "http://172.30.10.67:8000/modules/wikipedia_for_schools/index.htm" Page not found, my urls.py is just
urlpatterns = [
url(r'^$', views.IndexView, name='index'),
and i think it's impossible to rewrite each file from wikipedia offline project to Django MVT model to provide urls, models and templates for each link.
That's how my Django project structure looks like:
And that's how offline Wikipedia index page looks
Any help would be highly appreciated

Standalone HTML pages are static files, and should be stored in the static folder and accessed via /static/.

Related

Django webpage loading issue

I'm creating a project in Django, my admin section is working properly, and models are working properly but when I'm going to open the server to check the landing page, it shows like this attached below.
I've created another page and made that page the main landing page but that showed me the same result. Don't know what to do now.
if you create new app in Django . You need to introduce the new path to the Django framework so that the desired page is displayed in the output.
To introduce the new route, refer to the main URLs file of the app and use include:
path('', include('app_name'))

Django open pdf on certain page number

I am trying to create a PDF analysis web app and I am stuck. I want to allow the user to open a certain page of the pdf that have over 300 pages in it.
So, can anyone tell me how to use Django to open the pdf in a new tab on a specific page?
EDIT -- Actually the Django code is running on AWS server and I want the user to see and open a PDF on a specific page that is stored into my media folder after analysis.
The answer to this really isn't Django-specific and is going to depend on what viewer you are using to display the PDF. If you're relying on the browser to display it, according to Adobe, you can use an anchor link (#page=) in the url (e.g. http://www.example.com/myfile.pdf#page=XX) though I think support for this varies outside Firefox and Chrome.
If you're using PDF.JS, on the other hand, you'll be able to programatically select a page to render. You can see how to do that on the viewer's examples page, here.
This depends on which library etc.
Not really a django question as #ps_ said.
From Adobe.com
To target an HTML link to a specific page in a PDF file, add #page=[page number] to the end of the link's URL.
For example, this HTML tag opens page 4 of a PDF file named myfile.pdf:
<A HREF="http://www.example.com/myfile.pdf#page=4">
I was able to solve my question, it took a while and after every attempt to use different libraries and PDF.js failed.
The solution is quite simple. so what I did is:
Step 1: Add MEDIA_ROOT and MEDIA_URL to settings.py.
# path to the MEDIA directory
MEDIA_ROOT = '/home/absolute/path/to/your/media/folder'
# URL to use to open MEDIA
MEDIA_URL = '/media/'
Step 2: Update the urls.py with the following code:
from . import settings
import django
urlpatterns = [
...
...
url(r'^(.*?)media/(?P<path>.*)$', django.views.static.serve,
{'document_root': settings.MEDIA_ROOT}),
]
Step 3: Now I created an onclick in the HTML tag as:
My pdf files were in documents folder inside the media directory
onclick="window.open('media/documents/{{Your/Pdf/File/Path.pdf}}#page={{Page_number_to_open}}')"

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.

Django serving static pages from Sphinx

I have a large group of static html pages that I've generated from sphinx, and I'd like to show them on my Django site. I can connect to at least one page by putting the html/ folder form sphinx in a templates directory inside my app, and changing the urls.py file to include
url(r'^$', TemplateView.as_view(template_name="index.html")
Then it finds the index.html file inside myapp/templates/html. However, none of the internal links work (it'll try to redirect through Django and give me a 404 error). Also, the static files won't load in (Sphinx generates a _static folder, and even though I put that in the myapp directory, and the Chrome network tab tells me it's trying to load the css from myapp/_static, still nothing).
Is there any way to make all the links relative to each other inside this project? Alternatively, can I get Django to just serve up the whole project as static pages?
It looks like FlatPages is almost what I'm looking for, but I have more than just a title and content in these pages.
Firstly, you should not be using Django to serve essentially static files. This is what your webserver is for.
If you are using Apache, you can use the <directory> directive to serve these files alongside your Django routes.

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.

Categories