django-templates

how to pass the variable from included template to the template where it is included?

情到浓时终转凉″ 提交于 2019-12-02 18:07:51
问题 In Django Views:- if request.is_ajax(): t = get_template('bar-templates.html') html = t.render(Context({'edit': True, 'user':'some-user' })) return HttpResponse(html) There is two templates: Main template (foo-templates.html) which includes the template (bar-templates.html) . In context edit and user is passed to the bar-templates.html But this variable is also used in foo-templates.html . In django we used to {{ edit }} to catch the variable. Since this variable comes in bar-templates.html .

Django aggregation in templates?

喜欢而已 提交于 2019-12-02 17:56:08
问题 I'm thinking a bit about the concept of Django aggregates. I don't quite "get" how they can be used in my case. Basically i have a three-tier hierarchy of objects in my model, and the lowest object (Bar) contain values I want to aggregate. class Bar(models.Model): amount = models.FloatField() class Foo(models.Model): bars = models.ManyToManyField(Bar) class MyUser(models.Model): foos = models.ManyToManyField(Foo) I want my template to do the equivalent of this: {% for user in users %} <h2>{{

How do you use get_context_data with TemplateView in Django [closed]

删除回忆录丶 提交于 2019-12-02 17:55:09
I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl_books'] = Books.objects.filter(author="Dahl') When I try to access dahl_books in my template like this: {% for book in dahl_books %} dahl_books is not available in the template context, even though the Books QuerySet returned a non-zero number of books. ....am I doing something wrong in either my template or in get_context_data ? I can't test it, but I bet you need return context at the

Comma separated lists in django templates

无人久伴 提交于 2019-12-02 17:50:15
If fruits is the list ['apples', 'oranges', 'pears'] , is there a quick way using django template tags to produce "apples, oranges, and pears"? I know it's not difficult to do this using a loop and {% if counter.last %} statements, but because I'm going to use this repeatedly I think I'm going to have to learn how to write custom tags filters, and I don't want to reinvent the wheel if it's already been done. As an extension, my attempts to drop the Oxford Comma (ie return "apples, oranges and pears") are even messier. First choice: use the existing join template tag. http://docs.djangoproject

tree structure of parent child relation in django templates

烂漫一生 提交于 2019-12-02 17:47:58
how do i implement the tree structure in django templates with out using django-mptt. i have model. class Person(TimeStampedModel): name = models.CharField(max_length=32) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') now i want .. Parent Child 1 subchild 1.1 subchild 1.2 nextsubchild 1.2.1 Child 2 Child 3 there names should be click able to show their profile. DTing from Django while loop question and http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags # view.py @register.inclusion_tag('children.html') def children_tag(person):

How to render an ordered dictionary in django templates?

女生的网名这么多〃 提交于 2019-12-02 17:26:36
I'm trying to learning django templates but it's not easy. I have a certain views.py containing a dictionary to be rendered with a template. The dictionary is made of key-value pairs, where key are unique names and values are some values associated to those names. I render the dictionary in the following way: return render_to_response('results.html', {'data': results_dict}) Now my problem is that in my template I need to display the names in alphabetical (or ASCIIbetical) order with the relatives values. Actually in my template I have: <table> {% for key, value in data.items %} <tr> <td> {{

How do I get all the variables defined in a Django template?

戏子无情 提交于 2019-12-02 17:16:29
I'm new to Django and I wonder if there is a way to dump all the variables available to a template for debugging purposes. In Python I might use something like locals() , is there something equivalent for the default template engine? Note: suppose I don't have access to the view for the purposes of this question. Both Ned's and blaine's answers are good, but if you really want to achieve exactly what you ask for there's a template tag for it: {% debug %} Builtins:debug More information in the context_processor.debug including: If this processor is enabled, every RequestContext will contain

Django simple tag doesn't work in if condition

拈花ヽ惹草 提交于 2019-12-02 16:50:23
问题 I want to customize django-admin's change form for video objects by adding block with moderation tools. When I use custom simpletags in if condition - it doesn't work. models.py: class Video(models.Model): class Meta: db_table = 'video' DRAFT = 1 MODERATION = 2 PUBLISHED = 3 REJECTED = 4 HOSTING_UPLOADING = 5 SUSPICIOUS = 6 PUBLICATION_STATUSES = ( (DRAFT, 'draft'), (MODERATION, 'moderation'), (PUBLISHED, 'published'), (HOSTING_UPLOADING, 'hosting uploading'), (REJECTED, 'rejected'),

Django: Hide button in template, if user is not super-user

跟風遠走 提交于 2019-12-02 16:36:43
How do you get your template/view to recognize whether or not a logged in user is a super user or not? There are certain buttons on my forms (in the template) that I want completely hidden if the user is not a super-user How would you go about doing that? Check out is_superuser on the User object: {% if request.user.is_superuser %} ... <button>...</button> ... {% else %} ... {% endif %} EDIT: after @mustafa-0x comments The above assumes that you have django.core.context_processors.request included in your TEMPLATE_CONTEXT_PROCESSORS setting which isn't the default . The default setting for

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/

最后都变了- 提交于 2019-12-02 16:26:44
问题 I was following the django documentation and making a simple poll app. I have come across the following error : Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^polls/ ^admin/ The current URL, , didn't match any of these." settings.py ROOT_URLCONF = 'mysite.urls' mysite/mysite/urls.py from django.conf.urls import include,url from django.contrib import admin urlpatterns = [ url(r'^polls/',include('polls.urls')), url(r'^admin/', admin.site.urls),]