django-templates

How do I call a Django function on button click?

夙愿已清 提交于 2019-12-17 10:24:32
问题 I am trying to write a Django application and I am stuck at how I can call a view function when a button is clicked. In my template, I have a link button as below, when clicked it takes you to a different webpage: <a target="_blank" href="{{ column_3_item.link_for_item }}">Check It Out</a> When the button is clicked, I also want to call a Django view function (along with re-direct to a target website). The view function increments the value in the database which stores the number of times the

Django templates: value of dictionary key with a space in it

前提是你 提交于 2019-12-17 09:46:31
问题 In a Django template, is there a way to get a value from a key that has a space in it? Eg, if I have a dict like: {"Restaurant Name": Foo} How can I reference that value in my template? Pseudo-syntax might be: {{ entry['Restaurant Name'] }} 回答1: There is no clean way to do this with the built-in tags. Trying to do something like: {{ a.'Restaurant Name'}} or {{ a.Restaurant Name }} will throw a parse error. You could do a for loop through the dictionary (but it's ugly/inefficient): {% for k, v

{% include %} vs {% extends %} in django templates

旧街凉风 提交于 2019-12-17 09:36:13
问题 When particularly extend template and when to use include ? Is include of any use with content like user profile section (like about me in the corner of our site) ? 回答1: Extending allows you to replace blocks (e.g. "content") from a parent template instead of including parts to build the page (e.g. "header" and "footer"). This allows you to have a single template containing your complete layout and you only "insert" the content of the other template by replacing a block. If the user profile

Get type of Django form widget from within template

落花浮王杯 提交于 2019-12-17 08:32:08
问题 I'm iterating through the fields of a form and for certain fields I want a slightly different layout, requiring altered HTML. To do this accurately, I just need to know the widget type. Its class name or something similar. In standard python, this is easy! field.field.widget.__class__.__name__ Unfortunately, you're not allowed access to underscore variables in templates. Great! You can test field.field.widget.input_type but this only works for text/password <input ../> types. I need more

Reverse for '*' with arguments '()' and keyword arguments '{}' not found

喜你入骨 提交于 2019-12-17 07:10:25
问题 Caught an exception while rendering: Reverse for 'products.views.'filter_by_led' with arguments '()' and keyword arguments '{}' not found. I was able to successfully import products.views.filter_by_led from the shell and it worked so the path should be correct. Here is the urls.py: (r'^led-tv/$', filter_by_led ), This is where the error is being generated: href="{% url products.views.filter_by_led %}"> Which I can't understand because this works fine from the same file: {% url products.views

How do I display the value of a Django form field in a template?

故事扮演 提交于 2019-12-17 06:25:08
问题 I have a form with an email property. When using {{ form.email }} in case of some validation error, Django still renders the previous value in the input tag's value attribute: <input type="text" id="id_email" maxlength="75" class="required" value="some@email.com" name="email"> I want to render the input tag myself (to add some JavaScript code and an error class in case of an error). For example this is my template instead of {{ form.email }} : <input type="text" autocomplete="on" id="id_email

Get list item dynamically in django templates

此生再无相见时 提交于 2019-12-17 06:09:32
问题 I have some loop on the page and need list item depending from loop number. When I call: {{ mylist.1 }} {{ mylist.2 }} {{ mylist.3 }} all works fine but what I really need is something like: {% for x in somenumber|MyCustomRangeTag %} {{ mylist.x }} {% endfor %} MyCustomRangeTag gives me Python range() it works and I already have x as number. So x is 1, 2, 3 etc. depending from loop number. Is this possible and how? 回答1: This is not possible directly because Django thinks that "x" is the key

Iterating through two lists in Django templates

蓝咒 提交于 2019-12-17 05:03:28
问题 I want to do the below list iteration in django templates: foo = ['foo', 'bar']; moo = ['moo', 'loo']; for (a, b) in zip(foo, moo): print a, b django code: {%for a, b in zip(foo, moo)%} {{a}} {{b}} {%endfor%} I get the below error when I try this: File "/base/python_lib/versions/third_party/django-0.96/django/template/defaulttags.py", line 538, in do_for raise TemplateSyntaxError, "'for' statements should have either four or five words: %s" % token.contents How do I accomplish this? 回答1: It's

Need to convert a string to int in a django template

匆匆过客 提交于 2019-12-17 05:02:20
问题 I am trying to pass in url parameters to a django template like this... response = render_to_string('persistConTemplate.html', request.GET) This the calling line from my views.py file. persistConTemplate.html is the name of my template and request.GET is the dictionary that contains the url parameters. In the template I try to use one of the parameters like this... {% for item in (numItems) %} item {{item}} {% endfor %} numItems is one of the url parameters that I am sending in my request

How do I add multiple arguments to my custom template filter in a django template?

ε祈祈猫儿з 提交于 2019-12-17 04:47:47
问题 Here's my custom filter: from django import template register = template.Library() @register.filter def replace(value, cherche, remplacement): return value.replace(cherche, remplacement) and here are the ways I tried using it in my template file that resulted in an error: {{ attr.name|replace:"_"," " }} {{ attr.name|replace:"_" " " }} {{ attr.name|replace:"_":" " }} {{ attr.name|replace:"cherche='_', remplacement=' '" }} I looked into django's docs and book but only found example using a