Django admin add related object doesn't open popup window? - django

The django-admin app is supposed to open a popup window when I try to create a related object. For example, clicking the green plus button here:
Should bring a new pop-up window, where I can create a new related (in this case Session) object. However, when I click the green plus button, the object create opens up in the same window, instead of a pop-up window. This is a problem, because if a user starts to create a primary object, then half-way through, clicks the green plus, he will lose all the progress on the primary object. This means that the green plus is unusable.
I have no idea why this is happening. I understand that I am not providing a minimum example of the problem, because I haven't been able to construct one. Right now I want to diagnose the problem. Any ideas of how I can diagnose this? And fix it?
I can provide any information you think is necessary.
Related: Django admin popup links are broken. But he's using Grappelli, which I am not. I am using the default django admin interface.
I am using Django version 1.8.4.
UPDATE: The popups work fine with manage.py runserver. The problem only appears with the deployed version under apache. I hope this narrows down the issue.
On google groups: https://groups.google.com/forum/#!topic/django-users/awYelJjFjHk.

I wasn't able to produce a minimal example of what was going on, but it is fixed now. Essentially, I did the following:
Upgraded to Django 1.8.6 .
Ran python manage.py collectstatic (as suggested in https://community.webfaction.com/questions/18544/there-is-not-popup-window-for-one-to-many-relationship-with-djangos-admin-v18)
So in the end I'm not sure what the issue was. I hope this is useful to someone else.
If someone else comes up with a more complete answer, which involves a minimal example and with an explanation of what was going on, I'll be glad to change the accepter answer.

This appears to be a reproducible error in 1.9.3. Testing it myself I setup a new project using the latest Django (1.9.3) and ran into the same error. Below is the fix. It seems to also work with 1.9.1 I will report to Django team now.
+++ b/contrib/admin/templates/admin/related_widget_wrapper.html
## -10,7 +10,7 ##
</a>
{% endif %}
{% if can_add_related %}
- <a class="related-widget-wrapper-link add-related" id="add_id_{{ name }}"
+ <a class="related-widget-wrapper-link add-another" id="add_id_{{ name }}"
href="{{ add_related_url }}?{{ url_params }}"
title="{% blocktrans %}Add another {{ model }}{% endblocktrans %}">
<img src="{% static 'admin/img/icon-addlink.svg' %}" alt="{% trans 'Add' %}"/>

Related

Django widget tweaks popover won't move with content

I'm using django widget tweaks to render a form field that is required. It all works fine, if the field is blank, I see a cute little popover that says field is required and all that, but if I scroll the page (the form is a little big), the popover won't move with the form field. It'll stay put and that is not good.
Here's my code:
{% load widget_tweaks %}
{{form.order_number.label}}
{% render_field form.order_number required="true" %}
Also, this is happening only on Firefox, not on Chrome. I'm on Firefox 57.0. Here's a screenshot to help. In Pic1, you'll see it is supposed to be where I like it without scrolling. In Pic2, it has gone way upwards to the top of the div when I scroll up.
Could someone please explain why this is happening and how I can fix it?
This is the behaviour of the browser when an input has the required attribute, so you can't do much about it (changing the browser's behaviour itself).
What you can do is override the default's browser with some javascript validation library, which let's you customize the type and looks of the messages as you need.
For example http://www.formvalidator.net/

Combining Django Templates and Polymer

I've been stuck for the past few hours trying to figure out why the core Polymer elements are not being displayed properly in a Django application I'm making to act as a personal webpage. The application at the moment just points to an index.html page which, if you follow the tutorial on Polymer, is up to step one.
However, the components are not loading on my page. The static files are all set up correctly, and there's subtle animation from the css files being loaded correctly, but the Roboto font and the core-elements are not being displayed. Running the site as a normal HTML file does everything correctly.
Is there a specific way to user Polymer in a Django template?
Thanks.
See Eric's answer to this on the polymer-dev mailing list: https://groups.google.com/forum/?fromgroups=#!searchin/polymer-dev/django/polymer-dev/N2R8qknalOI/58ZhC1gWFh4J
Relevant excerpt:
Django 1.5 has support for the verbatim tag. You can wrap your inlined element definitions in that:
https://docs.djangoproject.com/en/1.5/ref/templates/builtins/#verbatim
Example code snippet:
{% verbatim %}
<template repeat="{{item as items}}">
<my-element name="{{item.name}}"></my-element>
</template>
{% endverbatim %}
<script>
document.querySelector("template').model = {{items}}; // items here is filled by the server's template.
</script>
I'm pretty sure this has to do with the fact that Django uses the same {{}} in its templates as Polymer.
I'm adding this answer as a compliment to the already accepted answer.
You can force django to require a space for it's template tags. So for any django template tags you have to use {{ variable }} and for polymer you will use {{variable}}.
Here is a very simple module/app I created to "prepare" django for use alongside polymer.
https://github.com/andrewebdev/django-ostinato/blob/2c435dea23319be6e9011e7381afca2b4092b5a2/ostinato/polyprep/init.py
Credit goes to https://github.com/nebrybledu for this suggestion.

django-autocomplete-light links cannot target iframe

Sorry up front if my terminology will be non-standard, but I am new to Django and Python...I am using Python 2.7.3 and Django 1.4.3 on a Mac OSX 10.7 (Lion). Django-autocomplete-light version 1.1.7.
I am using the django-autocomplete-light module to create a page where I can search within a set of videos (hosted on an external source like Youtube). I defined a model / app called Videos that contains a set of videos hosted on this third-party site. At the top of my videos/index.html page, I have a search box that is powered by the django-autocomplete-light module. I have gotten the overall search function working--when I type something into the search box, the relevant videos are shown in the drop-down menu automatically. I want the clicked video to play within an embedded player on the page--for some reason, this keeps on loading the video into the entire, existing document / page instead of just the iFrame. In my structure for the search box, I included the "target" attribute already. My understanding is that including "target" should work, but I must be missing something. When I check Firebug, the "target" attribute is included in the link in the search results (sorry, can't post a screenshot of this since I don't have enough reputation points...but it's there. The iFrame also has its "name" attribute assigned to the same thing).
Side note--I am hosting the video on a private service (not Youtube), so I prefer not to use a library like jQuery TubePlayer...but the concept remains the same).
My questions--is there another way to do this? Am I missing something obvious? Does django-autocomplete-light not play nice with iFrames?
(templates/videos/index.html)
<iframe name="tvplayer" width="560" height="315" src="http://www.youtube.com/embed/CE0Q904gtMI" frameborder="0" allowfullscreen></iframe>
(navigation_autocomplete/autocomplete.html)--I understand that all videos will return the same link using this code, but I can't even get this one to work!
{% for video in videos %}
<a style="display:block" href="http://www.youtube.com/embed/3svKWr9LH4I" target="tvplayer">{{ video.title }} - {{ video.desc }}</a>
{% endfor %}
Thanks for your help!
Solved my own question...within the navigation_autocomplete/script.html page, you have to change the navigation_autocomplete javascript handler directly since it overrides any other styling / parameters that you put into the a tag.

How to load objects from model on server start up?

I store my sites navigation menu in database. I want to load all objects in the list once I launch my server, but I haven’t got idea how to do it. I really need to do it, because it will be problem in future to load data from database in views to display menu.
I tried to put loading code in settings.py, but there was an error, and in views after imports, but there was no effect.
I think you need Template context processors.
Here is a nice tutorial by James Bennett on that:
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
Well, I'm very new to django, and maybe I'm wrong, but I think you are looking for something like caching. Read the docs, and decide whether is it fits you or not.
You can cache just a part of your view:
https://docs.djangoproject.com/en/1.3/topics/cache/#template-fragment-caching
surround your navigation bar like this:
{% cache 500 navbar %}
... put your navbar code
{% endcache %}
and ensure to have
{% load cache %}
at the top of your template or the base template.

Komodo Edit 5.2 Django Template Syntax Error - Info: <head> previously mentioned

I am using Komodo Edit 5.2 for editing html and Django template files. It always shows a single syntax error inside the first {% block %} area on the first tag of my template.
For example:
{% extends "base.html" %}
{% load i18n %}
{% block title %}Hello{% endblock %}
{% block content %}
<p>Hello</p> <-- Syntax error on this single line
<p>Other lines have no errors</p>
{% endblock %}
{% block footer %}
<p>No errors here</p>
{% endblock %}
The syntax error given is:
Info: <head> previously mentioned
I know for a fact that the error has nothing to do with my <head> tag since it occurs in the base template and in child templates (and the IDE isn't smart enough to process the base templates when in a child, etc.) All of my html tags are closed properly and everything validates for XHTML strict.
This forum post mentions a similar problem but offers no solution (and may be specific to Smarty syntax highlighting).
Any ideas on how to resolve this error (or disable it from being shown)?
Yes, this can be fixed, but it's really rather ugly a method.
I should point out this is a bug that's in the tracker #77251 (edit: this is fixed in Komodo 6.1.0). You could watch that bug to find when it actually gets fixed.
As for stopping the red squiggly lines -- You can do this by editing the python language file for Django template HTML manually. The file you want to edit is koDjango_URL_Language.py, and can be found in ..ActiveState Komodo Edit 5\lib\mozilla\extensions\django_language#ActiveState.com\components.
Add the following override method to class KoDjangoLanguage:
def get_linter(self):
return None
Note that this assumes you are saving your django template files as .django.html (which on a side note if you have not, doing so provides template-tag syntax coloring, which is kinda nice). After editing the file, save, and reload Komodo and the problem should go away.
From what I understand this file needs to be re-edited whenever you upgrade versions of Komodo as well. Hopefully we'll see sooner than later this whole issue get fixed and better all around support for Django in everyone's favorite IDE's.
If you're predominantly working on Django projects, you can change Komodo's default handling of *.html files by editing Preferences > File Associations and changing the association for *.html to "Django" (eg, as it usually is for *.django.html). This will save you having to rename all your template files.
To get rid of the squiggly red line, I use a custom Tidy config file, which can be specified under Preferences > Languages > HTML in the Configuration File chooser. The contents of this file are simply:
show-warnings: n
It's a bit of a hack, since it will be suppressing legitimate HTML warnings too. Until Komodo/Tidy natively supports the Django template tags however, it works for me.
If you're looking for a way of just hiding the squigly lines, rather than fiddling with the HTML Tidy configuration, try the following. In Preferences>Fonts and Colors, select the 'Indicators' tab. In the indicator selector there is an entry named 'Linter error'. Assign the style 'hidden' to it, and the squigly lines will be hidden. The statusline will still display the linter information. This is tested on Komodo Edit 5.2.4 on Linux.
As with renaming your templates files to *.django.html and using generic views you might run into TemplateDoesNotExist exceptions since django only looks for *_list.html and such as far as i'm concerned.
edit: Furthermore when renaming all templates don't forget to rename all
{% extends *.html %} to {% extends *.django.html %} appropriately.
I always disable HTML error checking when editing Django templates, as they're not wholly valid HTML as you may well have realised. I'm not sure if this can be done for Komodo Edit, but I know it can be done for Komodo IDE, so one might assume they're similar in this respect. Anyway, so the solution is a little annoying I'm afraid; you'll need to disable it for each file you edit (I don't know how to do this globally)...
I'm not on my work PC right now, but if memory serves me, there's an icon at the bottom of the window frame which you can right click to edit properties. Buried deep in there there should be an option to disable HTML syntax checking.
If you can't find the option then let me know and I'll try to fill in the gaps.