django-templates

How to make STATIC_URL work in external JS Files (Django)

孤人 提交于 2019-12-06 07:57:37
I am trying to use STATIC_URL in external javascript file. I was expecting the result as it is working in template, but I found that it is not working in external Javascript file. Please also tell me some work around to make STATIC_URL work in javascript file as it will make my project more manageable. Also I am sending many ajax request and i want to have something like url template tag in my JS file. Please let me know if you know solution of any of them. Thanks I use a simple Django app for making Python / Django settings available in javascript. https://github.com/incuna/django-settingsjs

How to convert a matplotlib.pyplot to a bokeh plot

与世无争的帅哥 提交于 2019-12-06 07:55:06
问题 I have been reading today about how to render a matplotlib.pyplot in a Django template. I found bokeh library and I was trying to convert my matplotib in a valid input to bokeh components. I read .to_boke method is deprecated . datos = np.random.randn(1000) ## Discretizamos el conjunto de valores en n intervalos, ## en este caso 8 intervalos datosbin = np.histogram(datos, bins=np.linspace(np.min(datos), np.max(datos), 9))[0] ## Los datos los queremos en tanto por ciento datosbin = datosbin *

Django - verbose_name from a template tag

痞子三分冷 提交于 2019-12-06 07:37:20
I need to display several models name & objects in a template Here is my view def contents(request): """Lists Objects""" objects = [ Model1.objects.all(), Model2.objects.all(), Model3.objects.all(), Model4.objects.all(), ... ] return render_to_response('content/contents.html', objs , context_instance=RequestContext(request) ) My template {% for objs in objects %} <div class="object"> <div class="object_name">{{ get_verbose_name objs.0 }}</div> <ul> {% for obj in objs %} <li>{{ obj }}</li> {% endfor %} </ul> </div> {% endfor %} And my template filter @register.simple_tag def get_verbose_name

Django: How to catch a specific exception in a class based view during template rendering?

妖精的绣舞 提交于 2019-12-06 06:46:47
问题 How do you catch a specific exception during Django template rendering in a class based view? I have a custom exception ImmediateHttpResponse which is intended to cause an immediate redirect in my class based view. I have tried: def dispatch(self, *args, **kwargs): try: return super(AppConnectionsView, self).dispatch(*args, **kwargs) except ImmediateHttpResponse as e: return HttpResponseRedirect(e.response) The exception I am trying to catch is raised in a template tag and so it seems the

Invoke Django template renderer in memory without any files from strings?

僤鯓⒐⒋嵵緔 提交于 2019-12-06 06:29:59
问题 I have built a Macro language for my users that is based upon the Django template language. Users enter into UITextFields their template/macro snippets that can be rendered in the context of larger documents. So I have large multi-line string snippets of django template code that should be populated with variables that are also stored in memory. I don't want to ever have to dump anything to files, I need to render these template How can I invoke the Django template renderer on a template that

How to start forming a django website and how django structures pages?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 06:16:34
I started a django project for my personal website to learn django. So far I've got my development environment set with everything I need and followed this great tutorial to create some basic data structures and templates. Now I would like to start using my html layout I made before and start implementing the functionalities to it. However I'm having hard time understanding how to accomplish this. I've mostly done java portal solutions before this where I could start the server, create some pages, set my theme for them and then add custom portlets (the functionalities/code) wherever I wanted.

Using ifequal in Django

孤街醉人 提交于 2019-12-06 06:12:40
I'm sure there is something silly I'm missing here, but I'm trying to use ifequal to evaluate a template variable. Here's my model: USER_TYPES = ( ('instructor', 'Instructor'), ('student', 'Student'), ) class UserProfile(models.Model): type = models.CharField( choices=USER_TYPES, max_length=12 ) user = models.ForeignKey( User, unique=True ) def __unicode__(self): return u'%s' % (self.type) ...and I'm using this in the template: {% ifequal user.userprofile_set.get student %} You're a student! {% endifequal %} When I simply print out {{ user.userprofile_set.get }} I get: student Not sure what I

django: how to pass css to form.as_ul

末鹿安然 提交于 2019-12-06 06:07:40
my form is class Form(forms.Form): ability = forms.ChoiceField(widget= forms.CheckboxSelectMultiple(), choices = SKILLS, required=False) i am displaying it as {{ Form.as_ul }} how do i pass a css ul class to this? Can i do it somewhere in the template? not sure how this works Lepi Unfortunately you have to create a new widget, but you can use the __init__ and render functions of the existing CheckboxSelectMultiple widget. Add a new ul_attrs parameter to the contructor. from django import forms from django.forms.util import flatatt from django.utils.safestring import mark_safe class

Putting a block inside another in Django

非 Y 不嫁゛ 提交于 2019-12-06 05:35:18
问题 I have a Django template that I want to extend in multiple places. In some the div must be inside a form, and in others it must not be. To do this I put a block above and below the div so I could add and in them respectively. Desired: <form> <div class="my_div"> {% block div_content %} ... {% endblock %} </div> </form> Template: {% block div_top %}{% endblock %} <div class="my_div"> {% block div_content %} {% endblock %} </div> {% block div_bottom %}{% endblock %} Looking at this I can't help

Django query to get User's favorite posts?

微笑、不失礼 提交于 2019-12-06 05:34:53
问题 I'm a little confused by the Django lingo. So I have 3 models: Post, UserProfile(User), Favorite. Favorite keeps track of which Posts a User has favorited. Post--->Favorite<---User/UserProfile Favorite model: class Favorite(models.Model): user = models.ForeignKey(User, unique=False) post = models.ForeignKey(Post, unique=False) def __unicode__(self): return self.user.username UserProfile model: class UserProfile(models.Model) : user = models.ForeignKey(User, unique=True) def get_favorites(self