django-templates

Django template in nested dictionary

那年仲夏 提交于 2019-12-11 02:47:22
问题 I am using Django template, and I met one problem with nested dictionary. Dict: result_dict = {'type_0' : {'file_name' : 'abc', 'count' : 0}, 'type_1' : {'file_name' : 'xyz', 'count' : 50}} and the template in my HTML file is: {% for type in result_dict %} {{ type }}, {{ type.file_name }} {% endfor %} How can I show the value of type_0 and type_1 only ? I've tried: {% for key, value in result_dict %} {{ key }}, {{ value }} {% endfor %} but it doesn't work. Thank you for your help. 回答1: Use

Django date filter not showing formatted timestamp in rendered template

感情迁移 提交于 2019-12-11 02:43:48
问题 I'm trying to format a timestamp into a date using Django's {{ timestamp|date:"d M Y" }} feature. I'm running into a problem where I can output and see the raw timestamp with {{ timestamp }} , but the formatting via filter isn't outputting anything when my page is rendered using {{ timestamp|date:"d M Y" }} . For example, right now the result of {{ timestamp }} - {{ timestamp|date:"d M Y" }} is 1317945600 - when I load the page. Any suggestions on what I might be doing wrong? 回答1: The problem

Django: problems while loading custom filters in the base template file while using template inheritance

前提是你 提交于 2019-12-11 02:27:36
问题 When doing the {% load custom_filters %} in the template, after {% extends "base.html" %} everything works fine, but when I move the load to the base.html template the filter gets a weird behaviour. This is my custom_filters.py : from django import template from django.template.defaultfilters import stringfilter register = template.Library() # To cut off strings at a specified character, at first occurance. Example: # time = 19:30:12.123456 # {{ time|cut:'.' }} # returns: 19:30:12 @register

Safely rendering user provided django templates

人走茶凉 提交于 2019-12-11 02:26:13
问题 So as per a previous question of mine I've decided to start a website which allows django designers to upload templates and css files. I'll provide a well defined set of context inputs and objects and then render the templates that the users provided. This will hopefully give newbies a large set of examples to work from and designers a good way to stretch their wings. I need a way to determine whether a template is "safe" to render. Hopefully making sure there are no malicious javascript,

Django template How to Convert numbers to Words

我只是一个虾纸丫 提交于 2019-12-11 02:25:26
问题 I am searching for a template tag to convert a value like 522 to five hundred twenty two only. Looking in docs i came across django.contrib.humanize which has a build in tag intword but it works only with number with numbers greater than a million. as a alternative solution i have created a tag using function from Python script to convert from numbers to words for printing cheques kindly suggest if any better solution possible. 回答1: Check out this library: num2words Straight from the docs: >>

Error 404 static files in django 2.0

随声附和 提交于 2019-12-11 02:12:41
问题 I'm trying to locate all my static files in django 2.0. But when i run the server only i got 404 errors this is my settings code STATIC_URL = '/static/' STATIC_ROOT = '/var/www/example.com/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) and my url code urlpatterns = [ path('admin/', admin.site.urls), path('', include('naturalUser.urls')) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 404 errors [11/Feb/2018 00:45:11] "GET /static/bower_components

How to add extra data to a Django model for display in template?

自古美人都是妖i 提交于 2019-12-11 01:58:25
问题 My Django Model: class myModel(models.Model): myIntA = models.IntegerField(default=0) My View: myModelList = myModel.objects.all() for i in range(len(myModelList)): myModelList[i].myIntB = i return render( request, 'myApp/myTemplate.html', Context( { "myModels": myModelList, } ) ) Is the above legal? You can see that I added a variable myIntB to each myModel object. However when I try to print myIntB in the template below, nothing shows up. How can I access myIntB from the template? It is not

javascript function not getting called

穿精又带淫゛_ 提交于 2019-12-11 01:55:56
问题 In a Django template, I have a function function show_graph(i){ $("#"+i+"seegraph").click(function(){ $("#"+i+"graph").show(); var qaa = {{ question_ids }}; var qid = qaa[i-1]; jQuery.ajax({ url: "/canvas/recreatechart/", type: "get", data: {qid: qid }, success: function(response){ var resp = jQuery.parseJSON(response); alert("chart1"+i); show_graph("chart1"+i, resp['chart_type'], resp['series_names'], JSON.stringify(resp['data1']), resp['answer'], resp['stories1'], resp['colors'], resp[

Django Template inheritance causes a bus error

筅森魡賤 提交于 2019-12-11 01:55:50
问题 I'm working in a multisite hierarchy in a Django template, whereby I need a master base template ( base/base.html ), for which I have several master templates extending from, such as base/base_twocol.html . And then I have templates that extend from those templates, such as base/base_twocol_SECTION . Then I need to have the same set of templates, which will deal with another site, but extending from those templates, such as another_site/base.html , another_site/base_twocol.html , another_site

Django: Nested variables in templates

扶醉桌前 提交于 2019-12-11 01:55:08
问题 In one of my django login templates, I have a line: <input type="hidden" name="next" value="{{ next|default:'{% url jobseeker_home %}' }}" /> And when I view the source code of the HTML page generated, I get the following for the above template line: <input type="hidden" name="next" value="{% url jobseeker_home %}" /> Unfortunately, the {% url jobseeker_home %} is not being resolved. How can I solve this? Thanks 回答1: Variables can be declared within a Django template: {% url jobseeker_home as