Executing code for a custom Django 404 page - python

I am getting ready to deploy my first Django application and am hitting a bit of a roadblock. My base template relies on me passing in the session object so that it can read out the currently logged in user's name. This isn't a problem when I control the code that is calling a template.
However, as part of getting this app ready to be deployed, I need to create a 404.html page. I extended my base template just like I've done with my other pages, but I don't see a way to pass in the session object so that I can utilize it. Is there a way to have Django call a custom method to render your 404 rather than just rendering your 404.html for you?

You need to override the default view handler for the 404 error. Here is the documentation on how to create your own custom 404 view function:
http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views

Define your own 404 handler. See Django URLs, specifically the part about handler404.

Related

Calling Django JSON API from Template

Say I have a django app called App1 and a django app called App2.
App1 has an endpoint called getJson() which returns a json object.
Now, in App2, I have an endpoint which renders an html template. In the html template I have a button and when the button is clicked, I want to call App1's getJson function. Is there a better way to do this than doing a get request in the JS of the template? If so, how?
Thanks!
Short answer, no. That's the basic flow of a Django App, you get requests and you respond.
The Django apps are running on a server machine. When you say App2 renders an html template, what it is doing is sending an HttpResponse with the given template, rendered, as payload to whoever requested it. This will be received in another machine, by another process.
So, if that other process (in another machine or the same) renders the text as an html, loads a website and shows a button, its not at all linked to your Django App. App2 ends its job when it renders and sends an HttpResponse. So if you want to control what ever happens when you press that button, you will need to communicate back with your server. And we do that sending http requests.

Pass js variable to Jinja template in Ckan

I want to pass javascript variable value to jinja2 template to query db according to some user requests.I can store user requests in js variable but I cannot pass variable value to Jinja.Do you have any idea or example?I use ckan project so I have to use Jinja template.I made some research maybe I have to use ajax request but how can I learn which function will be running in backend side as far as I know ckan use pyloans framework in backend side.How to render template in pyloans framework.Do you have any example?
As #E.Serra has already pointed out this is not possible: the Jinja-templates are executed on the server, while the JavaScript is executed afterwards in the browser.
Hence, you can either
compute the desired content when the page is first loaded and include it in your Jinja-template. For this, you would implement the ITemplateHelpers interface and implement a custom helper function which you can then call fro your template.
compute the desired content asynchronously after the page has loaded. For this you would send a web request from JavaScript to a CKAN API end point. If none of the existing API functions provide what you need then you can add your own using the IActions interface.

Django suppresses exception in production mode

I have a code that causes an exception (function called from template).
When DEBUG=True it correctly raises Exception.
But when DEBUG=False just blank page is displayed.
What should I do to disable such behavior?
If you want to show the Django debugging exception page in production, you shouldn't do this.
If you just want to create a custom error page, create a template called 500.html - this will be rendered instead of a blank page.

How to retrieve information from a Django template

I have a django template that calls a python methods but I can't seem to find a way for the python method to retrieve information from the template such as user input. Could anybody tell me how to do this?
I think you're under a misconception (A reasonably common one) that because you can use django variables in the html template, the template can "send" information back to django/the database.
Instead, try to get into the habit of thinking in terms of request/response. The user requests a web page, the django server builds up the response using content from the database and an html template (via the template language) and serves it to the user. If the response page has a form in it, that's not the "template" sending information back to the Django server, that's the response web page providing the user will tools to make ANOTHER request (this time a POST request).
The template is merely a generic container that you fill up with your dynamic content to build up the entire HTTP response.

How to create a custom 404 page for my Django/Apache?

I know that you use .htaccess in the document-root directory in standard Apache.
What if I use Django? Can someone give me step by step how to create a custom 404 page?
The default 404 handler calls 404.html . You could edit that if you don't need anything fancy or can override the 404 handler by setting the handler404 view -- more here

Categories

Resources