I'm trying to use the admin datepicker in my own django forms.
Roughly following the discussion here : http://www.mail-archive.com/django-users#googlegroups.com/msg72138.html
I've
a) In my forms.py included the line
from django.contrib.admin import widgets
b) and used the widget like this :
date = forms.DateTimeField(widget=widgets.AdminDateWidget())
c) And in my actual template I've added :
{{form.media}}
To include the js / styles etc.
However, when I try to view my form I get no nice widget; just an ordinary text box. And the Firefox javascript error console shows me :
gettext is not defined in calendar.js (line 26)
and
addEvent is not defined in DateTimeShortcuts.js (line 254)
Any suggestions? Is this a bug in Django's own javascript library?
Update : Basically, need to include the core and (or fake) the i18lization
Update 2 : Carl points out this is pretty much a duplicate of Using Django time/date widgets in custom form (although starting from a different position)
No, it's not a bug.
It's trying to call the gettext() internationalization function in js. You can do js internationalization much like you do it in python code or templates, it's only a less known feature.
If you don't use js internationalization in your project you can just put.
<script>function gettext(txt){ return txt }</script>
in your top template so the js interpreter doesn't choke.
This is a hacky way to solve it I know.
Edit:
Or you can include the exact jsi18n js django admin references to get it working even with other languages. I don't know which one it is.
This was posted on django-users today:
http://groups.google.com/group/django-users/browse_thread/thread/2f529966472c479d#
Maybe it was you, anyway, just in case.
I think I solved the first half by explicitly adding these lines to my template :
<script type="text/javascript" src="../../../jsi18n/"></script>
<script type="text/javascript" src="/admin_media/js/core.js"></script>
<script type="text/javascript" src="/admin_media/js/admin/RelatedObjectLookups.js"></script>
But it still reports not knowing gettext
You may find the following works for you:
<link href="/media/css/base.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/media/js/core.js"></script>
{{ form.media }}
Related
I try to connect a Django channels Consumer to a HTMX ext-ws element, but I can't get a step further.
class MessageConsumer(WebsocketConsumer):
def connect(self):
self.accept()
print("connect")
#self.send(
# "type": "websocket.send",
# "text": "..."
#)
...
<head>
...
<script src="{% static 'common/js/htmx/htmx.min.js' %}" defer></script>
<script src="{% static 'common/js/htmx/ext/ws.js' %}" defer></script>
...
</head>
...
The HTMX.js and the ws.js gets loaded correctly at the client's browser.
<div id="messages-container"
hx-ws="connect:/ws/messages/"
{# hx-ext="ws" ws-connect="/ws/messages/" does not work at all #}
>
<div id="message"></div>
</div>
If I use the old HTMX-builtin hx-ws method, at least the websocket connects. But I can't get a message to the HTMX element (I thought the #message div should be replaced).
If I use the new-style (HTMX extension) syntax (hx-ext="ws" ws-connect...)
Can anyone point me to the right direction?
As per docs you need to also include hx-swap-oob="true" attribute into the html that you send back from websocket:
See example from htmx GH:
https://github.com/bigskysoftware/htmx/blob/master/test/servers/ws/server.go#L24
When using the plugin version, what worked for me was removing the defer attribute from htmx related script tags.
Not sure why including defer is causing the issue though.
Update:
A GH issue has been opened by OP:
https://github.com/bigskysoftware/htmx/issues/957
I have the next problem:
Im ceating a basic django webpage using HTML and CSS. The issue is that when I try to make the href from the HTML file to the CSS one:
<link rel="stylesheet" type="text/css" href="modelado.css">
It doesn't work correctly. my HTML file called "inicio.html" and "modelado.css" are in the same folder, but once I make the href, the console says this:
Not Found: /inicio/modelado.css
I think that the console is trying to find a folder called "inicio", but that's impossible since inicio is the HTML file from where I am running the program.
What I want to know is if there is another way to write the direction, because that directory doesn't exist.
I also think that this is a django related problem, because when I only use HTML, that line of code actually works when the files are in the same folder.
Thanks!
CSS is not a dynamic file (it is static) so by using
{% load static %}
<link rel="stylesheet" href="{% static 'yourstyle.css' %}">
you can load static files (like css).
More info here
Using Tornado, i want to create an html file and save it to re-use it later. Inside the html i want a reference to the bootstrap css in my static files.
My simplfied html looks like this, following Tornado/Python self.render("example.html") ignores CSS
<html>
<head>
<link rel="stylesheet" type="text/css" href="{{static_url('app/content/bootstrap.css')}}" />
</head>
<body>
{{name}}
</body>
</html>
My simplified .py looks like this:
import tornado.template
loader=tornado.template.Loader(r"C:\templateDirectory")
output_from_parsed_template= loader.load("template.html").generate(name="John")
# to save the results
file = open(r"C:\templateDirectory\result.html","w")
file.write(output_from_parsed_template)
file.close()
However, i get the message:
NameError: name 'static_url' is not defined
Many of the predefined names for use in Tornado templates come from RequestHandler, rather than the template system itself (because they need Application-level configuration). To use static_url as-is, you'll need the whole serving stack, not just a Loader and Template.generate (or you can dig into the code and reconstruct what it's doing).
Say a website www.example.com has a with id="section_two". This section is offscreen. I can make this website load with this section onscreen by calling the URL www.example.com#section_two.
Now, say I'm trying to return a static index.html file using Bottle.py, but specifically I'm trying to return it with a onscreen that would otherwise offscreen. How can I return a URL of the form www.example.com#section_two?
Do you mean that you want a user to receive a html page already scrolled to the necessary section? It is not possible without some in-browser JavaScript. You can add something like this to your html (with JQuery):
<head>
...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(function() {
$('#section_two').get(0).scrollIntoView();
});
</script>
</head>
I am using new recaptcha2 and everything seems to work flawlessly on all modern browsers, however I have a problem only with IE8.
Captcha load properly on second and next visit on the page, but never on first load or in Private Mode.
What is even more strange, google recaptcha 2 page demo doesn't have this problems. I am using django-nocaptcha-recaptcha package, which is based on recaptcha-client for python, so I believe my configuration is pretty standard.
Console doesn't show any errors. Divs are not populate by recaptcha code.
Google on their recaptcha2 developers guide suggest to use a snipet like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
django-nocaptcha-recaptcha use similar piece of code and also use a line like this:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
This suppose to cause a problem, since ascync and defer are supported in IE 10+. In my case, when I used:
<script src="https://www.google.com/recaptcha/api.js"></script>
Problem disappeared :)