django-templates

Django while loop

心不动则不痛 提交于 2019-12-05 02:59:31
问题 I wonder if there's any way to do a while loop in django (I think that's what I'm after)? What I'm trying to do is a nestled ul/li list. The list is generated by a for loop in a for loop. But since some elements in the second for loop has more child's I want to iterate or them to and so on until all child nodes are iterated out. Only way I found so far is to have another for loop. But this seems not to generic and quite repetitive. And I need to know how many "levels" of child's there are.

Django best practice with foreign key queries

∥☆過路亽.° 提交于 2019-12-05 02:17:47
问题 models.py class Category(models.Model): name = models.CharField(max_length=50) class SubCatergory(models.Model): parent_category = models.ForeignKey(Category) name = models.CharField(max_length=100) views.py def all_products(request): c = Category.objects.all() s = SubCatergory.objects.all() return render_to_response('all_products.html', {'c':c, 's':s}) all_products.html {% for category in c %} <h1>{{ category.name }}</h1> <ul> {% for sub in s %} {% if category.id == sub.parent_category.id %}

Django: How to display Validation errors not specific to a field?

泄露秘密 提交于 2019-12-05 01:56:44
I have errors raised in the form's clean method (not tied to a field). How do I display them in the template? I tried {{ forms.errors }} and {{ form.non_field_errors }} but neither worked. According to the docs , they go in a special field ( __all__ ) and should be accessed via the non_field_errors() method. At a guess, I'd say that method returns a sequence. 来源: https://stackoverflow.com/questions/1883469/django-how-to-display-validation-errors-not-specific-to-a-field

Django - How to use custom template tag with 'if' and 'else' checks? [duplicate]

社会主义新天地 提交于 2019-12-05 01:32:51
This question already has answers here : if..else custom template tag (4 answers) Closed 3 years ago . I have made a custom template tag for permissions using python: register = template.Library() @register.simple_tag def get_user_perm(request, perm): try: obj = Profile.objects.get(user=request.user) obj_perms = obj.permission_tags.all() flag = False for p in obj_perms: if perm.lower() == p.codename.lower(): flag = True return flag return flag except Exception as e: return "" Then I loaded and used this in my template like this: {% load usr_perm %} {% get_user_perm request "add_users" %} Which

TemplateSyntaxError 'staticfiles' is not a valid tag library'

放肆的年华 提交于 2019-12-05 01:32:42
I'm having a really strange issue trying to get the staticfiles taglib working in my application. I'm essentially getting the following error: 'staticfiles' is not a valid tag library: Template library staticfiles not found, tried django.templatetags.staticfiles,django.contrib.admin.templatetags.staticfiles Here's my template which is throwing this error: {% load staticfiles %} <html> <head> {% block stylesheets %} <link rel="stylesheet" href="{% static "styles/bootstrap-1.2.0.min.css" %}"> {% endblock %} <title>{% block title %}Tzibor{% endblock %}</title> </head> <body> <h1>It Works!</h1> {%

Problem loading custom template tags (Error: No module named x)

╄→尐↘猪︶ㄣ 提交于 2019-12-05 01:27:02
I am currently writing a few custom template tags but for some reason they will not load. My directory structure is as follows: MyProj | ----MyApp | |----templatetags | |----myapp_tags.py |----__init__.py In myapp_tags.py from django.template import Library, Node from myproj.myapp.models import Product register = Library() class LatestProductsNode(Node): def render(self, context): context['recent_products'] = Product.objects.all()[:5] return '' def get_latest_products(parser, token): return LatestProductsNode() get_latest_products = register.tag(get_latest_products) In settings.py INSTALLED

How do I use request.META.get('HTTP_REFERER') within template?

ⅰ亾dé卋堺 提交于 2019-12-05 01:22:12
I'd like to use request.META.get('HTTP_REFERER') within template. My template source: <!-- this is login.html --> {% extends "base.html" %} {% block title %}django bookmark- login{% endblock %} {% block head %}login{% endblock %} {% block content %} {% if form.errors %} <p>try again!</p> {% endif %} <form method="post" action=".">{% csrf_token %} <p><label for="id_username">username:</label> {{ form.username }}</p> <p><label for="id_password">password:</label> {{ form.password }}</p> <input type="hidden" name="next" value="/<!-- I WANT TO USE 'HTTP_REFERER' HERE -->" /> <input type="submit"

How do I get a “debug” variable in my Django template context?

て烟熏妆下的殇ゞ 提交于 2019-12-05 01:01:32
According to this SO post: How to check the TEMPLATE_DEBUG flag in a django template? if: A) my settings.py file has: TEMPLATE_CONTEXT_PROCESSORS = ['django.core.context_processors.debug',... and B) I use a RequestContext (as opposed to a Context) I should have a "debug" variable to my template context. However, I don't: when I do {{debug}} in a template, it renders as nothing (""). Is there anything else I'm missing that is necessary to get a "debug" var in the template context? Mark Lavin You also need to ensure the request's IP address is in the INTERNAL_IPS in your settings (which you

Displaying a timedelta object in a django template

点点圈 提交于 2019-12-05 00:46:21
I'm having trouble getting my django template to display a timedelta object consistently. I tried using the time filter in my template, but nothing is displayed when I do this. The timedelta object is shown as follows on the errors page if I use Assert False: time datetime.timedelta(0, 38, 132827) This displays the time difference as: 0:00:38.132827 I would like to only show the hours, minutes, and seconds for each timedelta object. Does anyone have any suggestions on how I can do this? I followed Peter's advice and wrote a custom template filter. Here's the steps I took. First I followed this

how to activate DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST

不羁的心 提交于 2019-12-05 00:30:46
问题 I read this "DJANGO.CORE.CONTEXT_PROCESSORS.REQUEST If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain a variable request, which is the current HttpRequest. Note that this processor is not enabled by default; you'll have to activate it. " from this page http://docs.djangoproject.com/en/dev/ref/templates/api/ But it seems there is no information how to activate this processor. Here is my original question Access request in django custom template tags