django-templates

Is there a Django template tag that lets me set a context variable?

前提是你 提交于 2021-02-04 12:07:55
问题 I want to be able to set variables in a template to string values. I wrote a tag, but it doesn't seem to change the context. The intended use is: {% define "a string" as my_var %} Update (solved): class DefineNode(Node): def __init__(self, var, name): self.var = var self.name = name def __repr__(self): return "<DefineNode>" def render(self, context): context[self.name] = self.var return '' @register.tag def define(parser, token): """ Adds a name to the context for referencing an arbitrarily

Is there a Django template tag that lets me set a context variable?

断了今生、忘了曾经 提交于 2021-02-04 12:07:52
问题 I want to be able to set variables in a template to string values. I wrote a tag, but it doesn't seem to change the context. The intended use is: {% define "a string" as my_var %} Update (solved): class DefineNode(Node): def __init__(self, var, name): self.var = var self.name = name def __repr__(self): return "<DefineNode>" def render(self, context): context[self.name] = self.var return '' @register.tag def define(parser, token): """ Adds a name to the context for referencing an arbitrarily

Is there a Django template tag that lets me set a context variable?

折月煮酒 提交于 2021-02-04 12:07:12
问题 I want to be able to set variables in a template to string values. I wrote a tag, but it doesn't seem to change the context. The intended use is: {% define "a string" as my_var %} Update (solved): class DefineNode(Node): def __init__(self, var, name): self.var = var self.name = name def __repr__(self): return "<DefineNode>" def render(self, context): context[self.name] = self.var return '' @register.tag def define(parser, token): """ Adds a name to the context for referencing an arbitrarily

Is there a Django template tag that lets me set a context variable?

◇◆丶佛笑我妖孽 提交于 2021-02-04 12:06:01
问题 I want to be able to set variables in a template to string values. I wrote a tag, but it doesn't seem to change the context. The intended use is: {% define "a string" as my_var %} Update (solved): class DefineNode(Node): def __init__(self, var, name): self.var = var self.name = name def __repr__(self): return "<DefineNode>" def render(self, context): context[self.name] = self.var return '' @register.tag def define(parser, token): """ Adds a name to the context for referencing an arbitrarily

How to display Foreign key Data in Django view page?

て烟熏妆下的殇ゞ 提交于 2021-01-29 14:01:33
问题 i have relation between Project and ProjectFloorPlan , and i want to display ProjectFloorPlan data in my template. Please let me know how i can display the ForeignKey data in my template. Here is my models.py file... class Project(models.Model): name=models.CharField(max_length=225) slug=models.SlugField(null=True, unique=True) def __str__(self): return self.name class ProjectFloorPlan(models.Model): project=models.ForeignKey('Project', related_name='projectfloorplan', on_delete=models

Extraction datas from forms

随声附和 提交于 2021-01-29 13:46:54
问题 I've just start to learn Django and now I'm trying understand how to insert datas in db, how to work with datas from templates and ect. My question is about extracting datas from form(Meta class) in template. Here is my code: model class Worker(models.Model): POSITION_SHOICE = ( ('MANAGER', 'MANAGER'), ('developer', 'DEVELOPER'), ('teamlead', 'TEAMLEAD'), ('pm', 'PM'), ('hr', 'HR'), ) f_name = models.CharField(max_length=50) s_name = models.CharField(max_length=50) position = models.CharField

Static files doesn't load anymore - Django

独自空忆成欢 提交于 2021-01-29 13:37:11
问题 In the beginning everything worked perfectly fine, but now I have a problem where my static files doesn't load. I think this is a very weird problem since I didn't really touch anything that could've had an influence on the problem when it happened. Everything else is working fine, I just can't get the static files for some reason. I sometimes get a 200 http response trying to get the static files like so: [20/Aug/2020 16:12:51] "GET /order/checkout/ HTTP/1.1" 200 2029 [20/Aug/2020 16:12:51]

Django and Chart.js Not Rendering Graph Correctly

試著忘記壹切 提交于 2021-01-29 09:01:23
问题 So I spent the weekend testing and searching SO and finally got Django and Chart.js working. Kind of. It renders the graph but the data is not correct. When I loop through the object, the results are not what I would expect. Maybe I'm blind from staring at this all weekend. Would appreciate any pointers. Here are my views... class ChartView(LoginRequiredMixin, TemplateView): template_name = 'Books/chart.html' def get_context_data(self, **kwargs): context = super(ChartView, self).get_context

How to access foreign key in a django template? (DetailView)

橙三吉。 提交于 2021-01-29 08:56:54
问题 I want to display products in a Campaign DetailView template. In my project each campaign has a shop which contains products, so the flow is like, Campaign --> Shop --> Products Campaign models.py class Campaign(models.Model): team = models.OneToOneField(Team, related_name='campaigns', on_delete=models.CASCADE, null=True, blank=True) shop = models.OneToOneField(shop_models.Shop, on_delete=models.CASCADE, null=True, blank=True) Shop models.py class Product(models.Model): title = models

How to handle a POST request from a Django form loaded via a custom template tag?

馋奶兔 提交于 2021-01-29 08:45:48
问题 I added a Django form to my Bootstrap nav bar to be included on every page, and it renders as it should with the appropriate values. The form was added using an inclusion_tag. However, I'm now at a loss as to how to handle the request from the form. Upon submission, whichever page the user was on should reload with updated content from the form submission. For more context, see my earlier question: How to place a django form in a nav bar so that it appears on every page? 回答1: Answering my own