django-templates

Django - Global navigation in base.html using model

时光总嘲笑我的痴心妄想 提交于 2019-12-12 21:09:08
问题 I have a number of templates that extend base.html . I want the base.html template to house my global navigation and have the text and links in the global navigation be based on a model Division (i.e. the CharField in the model will be used as the button text in the global nav, and the id will be used to build the URL). I thought tags might work, but what I end up with is this (yes, I'm new to Django and Python): current_tags.py from django import template # Import your model from libs

for loop iteration in django

戏子无情 提交于 2019-12-12 19:26:43
问题 My coding is: views def showThread(request, thread_id) post_list = Post.objects.filter(id = thread_id) post_likes = PostLikes.objects.all() return render_to_response('show.html',locals(),context_instance=RequestContext(request)) models: class Post(models.Model): subject = models.CharField(max_length = 250) body = models.TextField() thread = models.ForeignKey('self', null = True, editable = False ) Show.html: {% for post in post_list %} {{post.id}}{{post.subject}} {% endfor %} {% for post_like

custom filter - needs two arguments

╄→гoц情女王★ 提交于 2019-12-12 19:15:33
问题 In the project manage app I'm working on it should be possible to edit/delete a ticket if you are the owner of (i.e. the creator of) the ticket and/or the admin of the project the ticket belongs to. In the template for showing a project I want to use a custom filter to determine this, used as seen here: {% if ticket|owner_or_admin:user %} <p> <a href="{% url ticket_edit project.id %}">Edit</a> <a id="delete_link" href="{% url ticket_delete ticket.id %}">Delete</a> </p> {% endif %} Below is a

Syntax error whenever I put Python code inside a Django template

瘦欲@ 提交于 2019-12-12 19:12:46
问题 I'm trying to do the following in my Django template: {% for embed in embeds %} {% embed2 = embed.replace("<", "<") %} {{embed2}}<br /> {% endfor %} However, I always get an invalid block or some syntax error when I do anything like that (by that I mean {% %} code inside a loop). Python doesn't have {} to signify "scope" so I think this might be my problem? Am I formatting my code wrong? Edit: the exact error is: Invalid block tag: 'embed2' Edit2: Since someone said what I'm doing is not

Extending Hallojs in Django Wagtail With Edit Source Button

流过昼夜 提交于 2019-12-12 18:52:37
问题 I've been abel to add an an 'Edit Html' source button to my editor with the following hook: @hooks.register('insert_editor_js') def enable_source(): return format_html( """ <script> registerHalloPlugin('hallohtml'); </script> """ ) It adds a button, but I can't figure out how to add an icon - see screenshot below with no icon. All buttons except no icon make the source editor work great. Thank you for your help. 回答1: Use the insert_editor_css hook to provide additional CSS files to the editor

Right way to escape JSON data from django template

耗尽温柔 提交于 2019-12-12 18:33:30
问题 I want to pass a dictionary from django view to a javascript file. The dictionary is built from a database populated by site users. What's the difference between these 2 methods in terms of security? var mydata = JSON.parse("{{mydata|escapejs}}"); var mydata = {{ mydata|safe }}; Further, the doc at django says this for escapejs : This does not make the string safe for use in HTML . Could you show me an example of how it's unsafe & how can I make it safe. 回答1: The following dictionary can

GWT set variable values with Django templates

限于喜欢 提交于 2019-12-12 18:08:34
问题 I am using a tree control in my GWT application. The tree has different items based on the user logged in. One way to populate the tree would be to query the server from my GWT code to get a list of tree items. But since the items will always be shown would it not be better to include information regarding them in the page itself? I am looking for views to achieve this. Would it be possible to get a template engine like django to insert these values into GWT string variables at serving time?

Django HTML truncation

蓝咒 提交于 2019-12-12 15:51:13
问题 I'm using the build-in truncatewords_html filter of Django and it adds "..." in the end, instead, I want to replace this with a link "See More". How can I achieve this? 回答1: It would be best to write your own filter. You could take the source code for truncatewords_html and use it as a template for your filter. It should take a few changes to get what you want, and then you will just need to register your template and make sure you load it on the page you want to use it on and you should be

how to return redirect to previous page in Django after POST request

强颜欢笑 提交于 2019-12-12 10:20:54
问题 I'm coding a news website,in the detail news page ,there is a comment fountain,if people want to post comment they need to login first.And I want to make it that,after they login in successfully ,the page can return to previous news page. Here is my views.py: def newsDetailView(request, news_pk): news = News.objects.get(id=news_pk) title = news.title author = news.author_name add_time = news.add_time content = news.content category = news.category tags = news.tag.annotate(news_count=Count(

AngularJS with Django - Conflicting template tags

感情迁移 提交于 2019-12-12 10:06:33
问题 I want to use AngularJS with Django however they both use {{ }} as their template tags. Is there an easy way to change one of the two to use some other custom templating tag? 回答1: For Angular 1.0 you should use the $interpolateProvider apis to configure the interpolation symbols: http://docs.angularjs.org/api/ng.$interpolateProvider. Something like this should do the trick: myModule.config(function($interpolateProvider) { $interpolateProvider.startSymbol('{[{'); $interpolateProvider.endSymbol