django-templates

Django strip_tags template filter add space

别来无恙 提交于 2020-01-02 19:45:32
问题 I use djangos template filter striptags. Example: >>> from django.utils.html import strip_tags >>> strip_tags("<p>This is a paragraph.</p><p>This is another paragraph.</p>") 'This is a paragraph.This is another paragraph.' What is the best way to add a space character between the paragraphs, so that I get this string instead: 'This is a paragraph. This is another paragraph.' Edit: One idea I have is to write a custom template filter that replaces all </p> tags with [space]</p> before the

Could not parse the reminder in django

梦想的初衷 提交于 2020-01-02 10:07:17
问题 I want to add a script tag inside a jquery template. So am referring to this link. It tells to close the inner script tag in the following way : <script id="filaVideoTemplate" type="text/x-jQuery-tmpl"> <!-- Some HTML here --> <script type="text/javascript"> <!-- Some javascript here --> {{html "</sc"+"ript>"}} </script> I tried it, but django shows this error: Could not parse the remainder: '" < /sc"+"ript>"' from 'html "< /sc"+"ript>"' .How can I do this django. Is there any specific

Django Static js files not working

孤人 提交于 2020-01-02 07:00:34
问题 Well my template code. <!DOCTYPE html> <html> <head> {% load staticfiles %} <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="{% static 'website/staffaddproblem.js' %}"></script> <title>Dashboard</title> <link href="{% static 'website/style.css' %}" rel="stylesheet" type="text/css"/> </head> <body> body </body> <html> As you can see it has a css file. which is working properly, but java script file does not work.

How to render a variable in a django template?

冷暖自知 提交于 2020-01-02 06:16:50
问题 My goal is to write dynamically some urls of images in the HTML page. Urls are stored in a database. To do so, first I am trying to render a simple varable in a template. Reading the docs and other sources, it should be done in 3 steps: For the configuration: in settings.py TEMPLATES = [ { 'OPTIONS': { 'debug': DEBUG, 'context_processors': [ … 'django.template.context_processors.request', 'django.template.context_processors.debug', 'django.template.context_processors.i18n', 'django.template

Where to put a Django template's dependent files?

落花浮王杯 提交于 2020-01-02 05:15:14
问题 My Django templates use a lot of related stuff: images, style sheets, etc. Where should I put these file, or how should I refer to them in the template itself? For now I'm using the development server. I know it's a really common thing, but I can't really figure it out. 回答1: I put them inside a folder named static , which is in the web project's top level folder. Example: /static/img/ /static/js/ /static/css/ /templates/ urls.py settings.py I then have the following rule in my urls.py file:

Django - getting Error “Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:” when using {% url “music:fav” %}

旧街凉风 提交于 2020-01-02 03:52:05
问题 I am learning django framework from last 4 days. Today I was trying to retrieve a URL in HTML template by using {% url "music:fav" %} where I set the namespace in music/urls.py as app_name= "music" and also I have a function named fav(). Here is the codes: music/urls.py from django.urls import path from . import views app_name = 'music' urlpatterns = [ path("", views.index, name="index"), path("<album_id>/", views.detail, name="detail"), path("<album_id>/fav/", views.fav, name="fav"), ] music

How To Use Django Cycle Tag

情到浓时终转凉″ 提交于 2020-01-02 03:36:05
问题 This hopefully is a pretty easy question. My endgoal is to be able to view my database entries in a table which I can sort via the column headers. I have read the documentation on cycle tags, but don't know they mean by 'row1' and 'row2' : {% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> ... </tr> {% endfor %} Secondly, how would I correctly implement it into my template? I am using a very simple JS library, which will allow the sorting: page.html {% if Variable %} <table class

Multiply by -1 in a Django template

泄露秘密 提交于 2020-01-02 01:53:06
问题 I'd like to always use a positive value of my variable in a Django template. The variable's sign is just a textual meaning: {% if qty > 0 %} Please, sell {{ qty }} products. {% elif qty < 0 %} Please, buy {{ -qty }} products. {% endif %} Of course, {{ -qty }} doesn't work. Is there a workaround without passing a second variable containing the absolute value? Something like a template filter that would convert the value to an unsigned integer. Thanks! 回答1: You can abuse some string filters: {%

What is the best practice for serving static files in Django currently

喜夏-厌秋 提交于 2020-01-01 17:00:29
问题 I've found plenty of advice for how to tackle static files in Django 1.x. Is there a best practices way to go about doing so? 回答1: There are may approaches to serving static files in Django, but Django 1.3 introduced a new option to handle them. Basically, you can define specific directories at a project level or at an app level that contain static media. Then via a management command, 'collectstatic', you can copy all of the static files in your project directory to a separate directory

How to test for use of a django template block?

有些话、适合烂在心里 提交于 2020-01-01 12:43:49
问题 I would like to do the following: {% if appnav %} <hr /> <div id="appnav"> <ul class="tabs"> {% block appnav %}{% endblock %} </ul> </div> {% endif %} ...however, testing for the present use of a block by templates further down the inheritance chain does not seem to work. Is there some other conditional that might do this? 回答1: The template language doesn't provide exactly what you're looking for. Child templates can call the parent block with {{ block.super }} , but parent templates can't