Django templating - listing available data to display - django

This is such a basic issue that I can only assume I'm looking at it completely the wrong way but...
I'm new enough to Django and I've taken the responsibility of redesigning a django application. The functionality is pretty good but the layout of things need to be styled and displayed a bit better.
My problem is this.
If in designing templates for for a PHP application and I want to see what attributes each of my objects have I can do a print_r (or a prettier inequivalent) and see exactly what data I can play with.
In django from the template level this doesn't appear to be possible which is baffling to me. Surely the idea of seperating design from business logic etc with MVC should also extend to have different people working on each level. If that is the case the a design person should have a way to display all variables available?

Use the django debug toolbar: http://github.com/robhudson/django-debug-toolbar
It requires minimal configuration after you install it and it will show you all kinds of useful information including the context for all the templates that were rendered, which is what you need.
To install it, in settings.py you put 'debug_toolbar', in INSTALLED_APPS and then add the following setting:
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
'SHOW_TOOLBAR_CALLBACK': lambda request: return DEBUG,
}
See the documentation for more details.
Once it is installed you should see a little tab DJDT on the right-hand side of your page. Clicking on it opens the toolbar and the template information is under Templates.

Django has a debug tag for this purpose.

Related

How to make editable fields in frontend for Django objects

I need to make objects editable in frontend by clicking on it and enable a form field to edit the text. i.e. in a ToDo-list which tasks can be edited by clicking on the task like shown in the following graphics:
I am working with Django 2.x and unfortunately, I'm a little inexperienced.
Is it a good practice to realize this with REST API and/or Angular/React?
I would be glad about a few experiences, how something is feasible.
I have found a good solution to implement the desired functionality with Jeditable
https://github.com/NicolasCARPi/jquery_jeditable

django-tinymce modern theme

I'm having issues getting the modern theme to work with django-tinymce. Both the simple and the advanced themes render correctly, but when I switch to the modern theme nothing renders and I get a 404 error for /static/tiny_mce/themes/modern/editor_template.js in the console
I am attempting to do all of this in the django admin. The error is coming from /static/tiny_mce/tiny_mce.js which is interesting because I don't have anything installed in that directory. I'm using /static/js/tinymce as my TINYMCE_JS_ROOT in settings.py. When switching between simple and advanced theme, everything works correctly.
I've tried to copy a version of editor_template.js in the exact location it's looking, but I still get the 404. It's like it wipes out /static/tiny_mce if it exists and replaces it with something, but I can't figure out how/where it's getting that from.
I'm using an install of TinyMCE 4.1.3 from http://www.tinymce.com/download/download.php and django 1.6.5
I've been struggling with TinyMCE recently, as well. I'm using TinyMCE v4 and Django 1.6. I went down the django-tinymce/django-flatpages-tinymce route because I had these working on another project. Some how it wasn't working for this new project. I did some research and decided to just go straight TinyMCE, no Django applications (eg, no django-tinymce or django-flatpages-tinymce).
This method cuts down on all configuration in Django, and it can be completely handled within the tinymce.init call. I found this much easier than dealing with Django's settings files, overriding models, etc. Just simply find the template you want TinyMCE to spice up and add the init call there.
The example here for full featured example really helped me:
http://www.tinymce.com/tryit/full.php
This use the modern theme...
I simply added this to whichever change_form.html template for whatever model I was needing the rich editor. For instance for flatpages:
admin/flatpages/flatpage/change_form.html
Or custom model in app:
admin/custom_app/model_name/change_form.html
I know this is exactly an answer to your question, but I think it's worth thinking about and might help you ultimately get what you need.
Also, I should note, it looks like only modern theme is available for TinyMCE v4:
http://www.tinymce.com/wiki.php/Tutorial:Migration_guide_from_3.x

Including a small app into a Django template

I am starting out in Django and haven't been able to find an answer to this question in tutorials or elsewhere. It seems like a simple feature, so I wonder if I am not understanding - so anyone that can explain it clearly would be greatly appreciated.
Say I have a site with 5 different pages via Django templates. If I write a small app that renders a poll (like the django tutorial), but I don't want that app to have its own url, just to feature, for example, in a sidebar of 2 of the 5 pages. The poll app has nothing to do with the other pages/apps.
My first thought was to go to those 2 pages' templates and include the template for the poll app - {% include "poll.html" %} ... I then found out this renders only the html as is, but I need to reference the database models to render a list of polls.
Inheritance doesn't quite make sense to me in this instance...
So, what would be the best practice way of doing this? For me, this is the whole idea of plug and play apps that Django is good for, but can't figure out how to just plug it in to other templates! I really just want to know the simple theory behind doing this, because it seems like a key feature.
Thanks!
Template tags - specifically, inclusion tags - are what you want.

What is the right way to organize a Django Project?

I'm new to Django and I'm trying to wrap my head around how these apps are supposed to be organized. I have the following questions:
Are apps like divs that are generated separately?
Can we have apps within apps?
Can we have apps that when clicked on, change other apps with javascript?
Right now I just have one views.py file and it loads all of its content through different function calls.
So right now I'm faced with if I should break up my views.py into smaller apps.
Am I going about Django the correct way?
Are apps defined like the they are in picture below, or are apps supposed to act more like a page?
What if I want a header, breadcrumbs, and footer for all my pages? I'm super confused #.#
Apps have nothing whatsoever to do with divs. Django is not a CMS (although it can be used to create CMSs) and doesn't dictate the layout of your templates.
The usual way to handle different blocks on the page that need different logic to populate them is via custom template tags. James Bennett has a good writeup on this, although the syntax is rather out of date so refer to the first link for that.

how to make interaction between different django apps in a single site?

I have just learnt about Django apps. I want to know that within one site, if I make different apps. like, users, profiles, polls, blogs,comments, jobs , applications then how can I manage them to make them intereactive? And is it how the concept of app should be? I want to have things loosely coupled that's why asking? Rails work on the way of REST, so do Django support that also with the use of apps? May be my questions seems a bit ambiguous because I am new to django and some of my concepts are still messed up.
Please tell what ever you know.
The general idea is that apps should be as loosely coupled as possible. The goal is to have completely self-contained functionality. Now, of course, that's not always possible and many times it even makes sense to bring in functionality from another app. To do that, you simply import whatever you need. For example, if your "blogs" app needed to work with your Comment model in your "comments" app you'd simply add the following to the top of the python file you're working in:
from comments.models import Comment
You can then use Comment as if it were defined right in the same file.
As far as REST goes, Django's views are much more fluid. You can name your view whatever you like; you need only hook it up to the right urlpattern in urls.py. Django views can return any content type, you just prepare the response and tell it what mimetype to serve it as (the default is HTML).