django-templates

Passing django variables to javascript

久未见 提交于 2020-08-25 10:05:27
问题 I'm having a difficult time passing variables from the python backend to javascript. A lot of my variables look like this in javascript: if ('{{ user.has_paid_plan}}' == 'True') { isPayingUser = true; } else { isPayingUser = false; } It's ugly and I'm sure there's a much cleaner way to do this. How should this be done? 回答1: This may be an odd approach, but I suppose one way to solve this would be to pass a json object as a variable, which would contain all other variables. For example: def

Passing django variables to javascript

让人想犯罪 __ 提交于 2020-08-25 10:00:14
问题 I'm having a difficult time passing variables from the python backend to javascript. A lot of my variables look like this in javascript: if ('{{ user.has_paid_plan}}' == 'True') { isPayingUser = true; } else { isPayingUser = false; } It's ugly and I'm sure there's a much cleaner way to do this. How should this be done? 回答1: This may be an odd approach, but I suppose one way to solve this would be to pass a json object as a variable, which would contain all other variables. For example: def

How can i update a django template in real time?

旧时模样 提交于 2020-08-25 03:46:39
问题 In this template i'm retrieving the price of Bitcoin from an API. At the actual moment, the price will be updated only when the page is refreshed, while i would like it to be updated without refreshing the whole page, dynamically. This is my view: def home(request): symbol = "BTCUSDT" tst = client.get_ticker(symbol=symbol) test = tst['lastPrice'] context={"test":test} return render(request, "main/home.html", context ) And the template's line looks something like this: <h3> var: {{test}} </h3>

How can i update a django template in real time?

自闭症网瘾萝莉.ら 提交于 2020-08-25 03:46:28
问题 In this template i'm retrieving the price of Bitcoin from an API. At the actual moment, the price will be updated only when the page is refreshed, while i would like it to be updated without refreshing the whole page, dynamically. This is my view: def home(request): symbol = "BTCUSDT" tst = client.get_ticker(symbol=symbol) test = tst['lastPrice'] context={"test":test} return render(request, "main/home.html", context ) And the template's line looks something like this: <h3> var: {{test}} </h3>

Django forms with Foreign keys

≡放荡痞女 提交于 2020-08-24 08:28:08
问题 I have scenario in which a user can have multiple books. I can create two different models for user and books and relate them using foreign keys (or one-to-many will be right way ?). I have created a django forms for User model but when i do like this {{form.as_p}} in templates only user model fields is shown not books field. I want that with user fields my books model filed also displayed (like book names field more then once because he can have multiple books) , Please let me know if it is

Dynamic updates in real time to a django template

安稳与你 提交于 2020-08-21 07:34:06
问题 I'm building a django app that will provide real time data. I'm fairly new to Django, and now i'm focusing on how to update my data in real time, without having to reload the whole page. Some clarification: the real time data should be update regularly, not only through a user input. View def home(request): symbol = "BTCUSDT" tst = client.get_ticker(symbol=symbol) test = tst['lastPrice'] context={"test":test} return render(request, "main/home.html", context ) Template <h3> var: {{test}} </h3>

How to filter product by Category wise in Django?

无人久伴 提交于 2020-08-10 19:12:30
问题 I am trying to filter according to the category but it's displaying all products on each category page, but I want filter according to the category page, please check my code and let me know how I can do it. here is my models.py file... class SubCategory(models.Model): subcat_name=models.CharField(max_length=225) subcat_slug=models.SlugField(max_length=225, unique=True) category = models.ForeignKey('Category', related_name='subcategoryies', on_delete=models.CASCADE, blank=True, null=True) and

Django filters to return HTML that will be rendered in the template

梦想的初衷 提交于 2020-08-07 07:53:29
问题 my_text my_text = '''The template system works in a two-step process: compiling and rendering. A compiled template is, simply, a list of Node objects. Thus, to define a custom template tag, you specify how the raw template tag is converted into a Node (the compilation function), and what the node’s render() method does.''' my_filter @register.filter(is_safe=True) def format_description(description): text = '' for i in description.split('\n'): text += ('<p class="site-description">' + i + '</p